javascript 實現禁止右鍵,複製,選取文本

來源:互聯網
上載者:User

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+nevent.returnValue=false;}if ((event.shiftKey)&&(event.keyCode==121)){ //屏蔽 shift+F10event.returnValue=false;}}</script></body 



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

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.