有時(shí)自己辛苦半天做的網(wǎng)頁,尤其是一些javascrīpt特效,很容易被人利用查看源文件而復(fù)制。那么如何才能防止被人查看源代碼呢?我們可以利用event.button特性來做到。下表是event.button屬性的可能取值及含義:
0 沒按鍵
1 按左鍵
2 按右鍵
3 按左和右鍵
4 按中間鍵
5 按左和中間鍵
6 按右和中間鍵
7 按所有的鍵
參照上表,我們可以在<body>和</body>之間加入如下語句:
<scrīpt Langvage=javascrīpt>
function Click(){
if (event.button!=1){alert('版權(quán)所有(C)2001 XXX工作室');
}}
document.onmousedown=Click;
</scrīpt>
這樣在瀏覽網(wǎng)頁時(shí)除單擊鼠標(biāo)左鍵外,其他任何形式的鼠標(biāo)點(diǎn)擊或組合點(diǎn)擊,都將出現(xiàn)“版權(quán)所有(C)2001 XXX工作室”的提示框,而不是出現(xiàn)快捷菜單,從而避免被人查看源文件代碼。
如果使event.button=2,實(shí)際上它僅能限制點(diǎn)擊鼠標(biāo)右鍵情況,其他點(diǎn)擊方式,如按左右鍵、按左和中間鍵、按中間鍵等就不能限制,當(dāng)這些方式的點(diǎn)擊發(fā)生時(shí),出現(xiàn)的就是快捷菜單,從而可以查看源文件。
注意:把body改為如下代碼:<body oncontextmenu="window.event.returnValue=false">,其中Value中的V一定要大寫!!
頁面禁用鼠標(biāo)右鍵代碼
可以把下面代碼加入到頁面適當(dāng)位置。
LeadBBS論壇應(yīng)用下面代碼時(shí),可以打開:inc/Board_Popfun.asp文件
查找:
<scrīpt language = "Javascrīpt" src = "<%=DEF_BBS_HomeUrl%>inc/JF.js" type="text/javascrīpt"></scrīpt>
下面加入代碼。
<scrīpt language=javascrīpt>
function openscrīpt(url, width, height,left,top,r){
var Win = window.open(url,"openscrīpt",'width=' + width + ',height=' + height + ',left=' +left+ ',top='+top+',resizable=no,scrollbars='+r+',menubar=no,status=no' );
}
//以下為禁止鼠標(biāo)右鍵的代碼,不想禁止的可以刪除
<!--
//www.vxbq.cn 網(wǎng)
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("【網(wǎng)】歡迎你"); } }
{
event.cancelBubble = true
event.returnValue = false;
return false;
}
}
document.oncontextmenu = nocontextmenu; // for IE5+
document.onmousedown = norightclick; // for all others
//-->
</scrīpt>
圖片禁用鼠標(biāo)右鍵代碼
應(yīng)用方法同上。
<scrīpt language="Javascrīpt1.2">
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 == 3) {
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()
</scrīpt>
功能:禁止右鍵、禁選擇、禁粘貼、禁shift、禁ctrl、禁alt
<scrīpt language="Javascrīpt">
<!--
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.onmousedown = norightclick; // for all others
//-->
</scrīpt>
<body onselectstart="return false"; onpaste="return false";>
如何用用javascrīpt 禁止右鍵,禁止復(fù)制,禁止粘貼,做站時(shí)常會(huì)用到這些代碼,所以收藏了一下!
1. oncontextmenu="window.event.returnValue=false" 將徹底屏蔽鼠標(biāo)右鍵特效
<table border oncontextmenu=return(false)><td>no</table> 可用于Table
2. <body onselectstart="return false"> 取消選取、防止復(fù)制 javascrīpt技巧
3. onpaste="return false" 不準(zhǔn)粘貼技巧
4. oncopy="return false;" oncut="return false;" 防止復(fù)制的javascirpt特效