有時自己辛苦半天做的網頁,尤其是一些javascript特效,很容易被人利用查看源檔案而複製。那麼如何才能防止被人查看原始碼呢?我們可以利用event.button特性來做到。下表是event.button屬性的可能取值及含義: 0 沒按鍵 1 按左鍵 2 按右鍵 3 按左和右鍵 4 按中間鍵 5 按左和中間鍵 6 按右和中間鍵 7 按所有的鍵 參照上表,我們可以在<body>和</body>之間加入如下語句: <script type="text/javascript"> function Click(){ if (event.button!=1){alert('XXXXXXX');} document.onmousedown=Click; </script> <script type="text/javascript"> //以下為禁止滑鼠右鍵的代碼,不想禁止的可以刪除 <!-- 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) {alert(" xxxxxxxxxxx"); } } { event.cancelBubble = true event.returnValue = false; return false; } } document.oncontextmenu = nocontextmenu; // for IE5+ document.onmousedown = norightclick; // for all others //--> </script> 圖片禁用滑鼠右鍵代碼 <script type="text/javascript"> var clickmessage="本站圖片禁用右鍵!" function disableclick(e) { if (document.all) { if (event.button==2||event.button==3) { if (event.srcElement.tagName=="IMG"){ alert(clickmessage); return false; } } } if (document.layers) { if (e.which == 2) { alert(clickmessage); return false; } } } function associateimages(){ for(i=0;i<document.images.length;i++) document.images[i].onmousedown=disableclick; } if (document.all) document.onmousedown=disableclick else if (document.layers) associateimages() </script> 功能:禁止右鍵、禁選擇、禁粘貼、禁shift、禁ctrl、禁alt <script language="JavaScript"> <!-- function key(){ //禁止Shift if(event.shiftKey){ window.close();} //禁止Alt if(event.altKey){ window.close();} //禁止Ctrl if(event.ctrlKey){ window.close();} 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.onmousedown = norightclick; // for all others //--> </script> <body onselectstart="return false"; onpaste="return false";> 如何用用javascript 禁止右鍵,禁止複製,禁止粘貼,做站時常會用到這些代碼:
1. <body oncontextmenu="window.event.returnValue=false" >將徹底屏蔽滑鼠右鍵特效 <table border oncontextmenu=return(false)><td>no</table> 可用於Table 2. <body onselectstart="return false"> 取消選取、防止複製 javascript技巧 3. onpaste="return false" 不準粘貼技巧 4. oncopy="return false;" oncut="return false;" 防止複製的javascirpt特效 |