屏蔽右鍵代碼(防止網頁惡意復制)


關於如何屏蔽右鍵復制的代碼。大概有如下幾種:
<head>語句下輸入

  1. <body onselectstart="return false;" oncontextmenu="return false;" >

系統首頁文件(default.asp)和日志文件(article.asp)最底下加入如下代碼即可.這兩個文件都在根目錄下.

  1. <noscript>
  2. <iframe scr="*.htm"></iframe>
  3. </noscript>
  4. <script language="JavaScript">
  5. document.oncontextmenu=new Function("event.returnValue=false;");
  6. document.onselectstart=new Function("event.returnValue=false;");
  7. </script>

優點是簡單易行,但保護不夠,很容易破解。
找到的一些辦法:
第一種 禁止右鍵、Ctrl鍵和復制功能的JS代碼

有的t網站頁面禁止使用右鍵和復制功能,甚至連Ctrl鍵也禁止掉了,這個效果是如何實現的呢?其實很簡單就是調用了一段JS代碼而已。
下面文本框中就是實現效果所需代碼:

  1. function click(e) {
  2. if (document.all) {
  3. if (event.button==1||event.button==2||event.button==3) {
  4. oncontextmenu='return false';
  5. }
  6. }
  7. if (document.layers) {
  8. if (e.which == 3) {
  9. oncontextmenu='return false';
  10. }
  11. }
  12. }
  13. if (document.layers) {
  14. document.captureEvents(Event.MOUSEDOWN);
  15. }
  16. document.onmousedown=click;
  17. document.oncontextmenu = new Function("return false;")
  18. var travel=true
  19. var hotkey=17 /* hotkey即為熱鍵的鍵值,是ASII碼,這里99代表c鍵 */
  20. if (document.layers)
  21. document.captureEvents(Event.KEYDOWN)
  22. function gogo(e)
  23. { if (document.layers) {
  24. if (e.which==hotkey&&travel){
  25. alert("操作錯誤.或許是您按錯了按鍵!"); } }
  26. else if (document.all){
  27. if (event.keyCode==hotkey&&travel){ alert("操作錯誤.或許是您按錯了按鍵!"); }}
  28. }
  29. document.onkeydown=gogo

把上面的代碼另存為一個JS文件,然后在想實現此效果的頁面用<!--#include file="*.js" -->調用即可,*代表你另存的文件名!
第二種 禁用右鍵並自動導航
腳本說明:
把如下代碼加入<body>區域中

  1. <script language="javascript">
  2. if (navigator.appName.indexOf("Internet Explorer") != -1)
  3. document.onmousedown = noSourceExplorer;
  4. function noSourceExplorer()
  5. {
  6. if (event.button == 2 | event.button == 3)
  7. {
  8. alert("禁止右鍵...去首頁!");
  9. location.replace("http://xiaoc.icpcn.com");
  10. }
  11. }
  12. </script>

第三種 禁用右鍵代碼
將以下代碼加到〈head〉與〈/head〉之間

  1. <script language="JavaScript">
  2. document.oncontextmenu=new Function("event.returnValue=false;");
  3. document.onselectstart=new Function("event.returnValue=false;");
  4. </script>

優點是使用了JS腳本,但是比較復雜,也很容易破解。

推薦使用的方法:
網頁腳本中(header.asp)插入以下代碼:

  1. <body oncontextmenu='return false' ondragstart='return false' onselectstart ='return false' onselect='document.selection.empty()' oncopy='document.selection.empty()' onbeforecopy='return false' onmouseup='document.selection.empty()'>

上面代碼的意思是當鼠標選中文字時為空
優點是代碼容易實現,不需要JS腳本的支持。且在一般的破解復制后,也會清空剪貼板。
其中,我去除了onmouseup='document.selection.empty()',因為這段代碼的意思是當鼠標鍵彈起時,選擇的內容為空,這樣就會影響正常的登陸哦。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM