javascript 实现禁止右键,复制,选取文本

时间:2022-11-06 23:23:33

1. JS 禁止右键

    <script type="text/javascript">
document.oncontextmenu=function(e){return false;}
</script>

<body onselectstart="return false">
......
2. CSS 禁止复制和选取
如果让整个页面都禁止选择
<style type="text/css">  
body {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
</style>
如果是局部
    <style type="text/css">      .unselectable {         -moz-user-select: -moz-none;         -khtml-user-select: none;         -webkit-user-select: none;               /*           Introduced in IE 10.           See http://ie.microsoft.com/testdrive/HTML5/msUserSelect/         */         -ms-user-select: none;         user-select: none;      }      </style>  
3. 完整实例:
    <style type="text/css">      body {          -webkit-touch-callout: none;          -webkit-user-select: none;          -khtml-user-select: none;          -moz-user-select: none;          -ms-user-select: none;          user-select: none;      }      </style>      <script langauge="javascript">      document.oncontextmenu=function(e){return false;}      </script>       </head>            <body onselectstart="return false">      ... ...  
或者:
    body{          -webkit-touch-callout: none;            -webkit-user-select: none;            -khtml-user-select: none;            -moz-user-select: none;            -ms-user-select: none;            user-select: none;        }            function iEsc(){ return false; }      function iRec(){ return true; }      function DisableKeys() {          if(event.ctrlKey || event.shiftKey || event.altKey)  {          window.event.returnValue=false;          iEsc();}      }            document.ondragstart=iEsc;      document.onkeydown=DisableKeys;      document.oncontextmenu=iEsc;            if (typeof document.onselectstart !="undefined") document.onselectstart=iEsc;      else      {          document.onmousedown=iEsc;          document.onmouseup=iRec;      }            function DisableRightClick(e)      {          if (window.Event){ if (e.which == 2 || e.which == 3) iEsc();}          else              if (event.button == 2 || event.button == 3)              {                  event.cancelBubble = true                  event.returnValue = false;                  iEsc();              }      }  


其他常用的JS代码(js禁止的一些功能)

禁止查看源文件
<script>
function clear(){
Source=document.body.firstChild.data;
document.open();
document.close();
document.title=”看不到源代码”;
document.body.innerHTML=Source;
}</script>

图片下载限制

<script language=”javascript”>
function Click(){
if(window.event.srcElement.tagName==”IMG”)
{
alert(‘图片直接右键’);
window.event.returnValue=false;
}
}
document.oncontextmenu=Click;
</script>
<pre name="code" class="html"><META HTTP-EQUIV=”imagetoolbar” CONTENT=”no”>  
插入图片时加入galleryimg属性
<img galleryimg=”no” src=””>
 
禁止右键保存

把下面代码放在<head>和</head>之间

<SCRIPT LANGUAGE=java script>
function click() {
alert(‘对不起,您不能保存此图片,谢谢您的理解和支持!’) }
function click1() {
if (event.button==2) {alert(‘对不起,您不能保存此图片,谢谢您的理解和支持!’) }}
function CtrlKeyDown(){
if (event.ctrlKey) {alert(‘不当的拷贝将损害您的系统!’) }}
document.onkeydown=CtrlKeyDown;
document.onselectstart=click;
document.onmousedown=click1;
</SCRIPT>
方式二:
在页面中加入如下js代码:原理:屏蔽右键
<script>
function document.onmousedown()
{
if(event.button==2||event.button==3)
{
alert( “右健被禁止 “)
return false
}
}
</script>
页面禁止刷新完全
最好在pop出来的窗口里用,没工具栏的
<body onkeydown=”KeyDown()” onbeforeunload=”location=location”
oncontextmenu=”event.returnValue=false”>

<script language=”Javascript”><!–
function KeyDown(){
if ((window.event.altKey)&&
((window.event.keyCode==37)||
(window.event.keyCode==39))){ alert(“请访问我的主页”);
event.returnValue=false;
}
if ((event.keyCode==8)|| (event.keyCode==116)){ //屏蔽 F5 刷新键
event.keyCode=0;
event.returnValue=false;
}
if ((event.ctrlKey)&&(event.keyCode==78)){ //屏蔽 Ctrl+n
event.returnValue=false;
}
if ((event.shiftKey)&&(event.keyCode==121)){ //屏蔽 shift+F10
event.returnValue=false;
}
}
</script>
</body



转自:http://www.chhua.com/web-note2825

http://justcoding.iteye.com/blog/1999249