Home  >  Article  >  Web Front-end  >  JavaScript代码实现禁止右键、禁选择、禁粘贴、禁shift、禁ctrl、禁alt_javascript技巧

JavaScript代码实现禁止右键、禁选择、禁粘贴、禁shift、禁ctrl、禁alt_javascript技巧

WBOY
WBOYOriginal
2016-05-16 15:32:061274browse

废话不多说了直接给大家贴代码了。

代码如下:

<script language="JavaScript">
<!--
//js禁用某些键的代码
//www.jb51.net
function key(){ 
if(event.shiftKey){
window.close();}
//禁止Shift
if(event.altKey){
window.close();}
//禁止Alt
if(event.ctrlKey){
window.close();}
//禁止Ctrl
return false;}
document.onkeydown=key;
if (window.Event)
document.captureEvents(Event.MOUSEUP);
function nocontextmenu(){
event.cancelBubble = true
event.returnValue = false;
return false;}
function norightclick(e){
if (window.Event){
if (e.which == 2 || e.which == 3)
return false;}
else
if (event.button == 2 || event.button == 3){
event.cancelBubble = true
event.returnValue = false;
return false;}
}
//禁右键
document.oncontextmenu = nocontextmenu; // for IE5+
document.onmou<a href="http://www.jb51.net/article/1141.html" target="_blank" class="infotextkey">sed</a>own = norightclick; // for all others
//-->
</script>


1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键特效

no
可用于Table

2.

取消选取、防止复制

3. onpaste="return false" 不准粘贴

4. oncopy="return false;" oncut="return false;" 防止复制

PS:JS防止后退,刷新,关闭的解决办法

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title> New Document </title>
</head>
<script language="javascript">
function RunOnBeforeUnload() {window.onbeforeunload = function(){ return '将丢失未保存的数据!'; } }
</script>
<body onload="RunOnBeforeUnload()">
刷新,关闭,后退,F5 测试
</body>
</html>

虽然onbeforeunload这个事件已经Web标准被淘汰,但目前能实现这个效果的也就只有这个事件.还好浏览器都能很好的支持.

测试结果:

IE6.0,FireFox,Chrome通过

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn