有时自己辛苦半天做的网页,尤其是一些javascript特效,很容易被人利用查看源文件而复制。那么如何才能防止被人查看源代码呢?我们可以利用event.button特性来做到。下表是event.button属性的可能取值及含义:
0没按键
1按左键
2按右键
3按左和右键
4按中间键
5按左和中间键
6按右和中间键
7按所有的键
参照上表,我们可以在<body>和</body>之间加入如下语句:
<ScriptLangvage=javascript>
functionClick(){
if(event.button!=1){alert('版权所有(C)2001 XXX工作室');
}}
document.onmousedown=Click;
</Script>
这样在浏览网页时除单击鼠标左键外,其他任何形式的鼠标点击或组合点击,都将出现“版权所有(C)2001XXX工作室”的提示框,而不是出现快捷菜单,从而避免被人查看源文件代码。
如果使event.button=2,实际上它仅能限制点击鼠标右键情况,其他点击方式,如按左右键、按左和中间键、按中间键等就不能限制,当这些方式的点击发生时,出现的就是快捷菜单,从而可以查看源文件。
注意:把body改为如下代码:<bodyoncontextmenu="window.event.returnValue=false">,其中Value中的V一定要大写!!
页面禁用鼠标右键代码
可以把下面代码加入到页面适当位置。
LeadBBS论坛应用下面代码时,可以打开:inc/Board_Popfun.asp文件
查找:
<scriptlanguage="JavaScript"src="<%=DEF_BBS_HomeUrl%>inc/JF.js"type="text/javascript"></script>
下面加入代码。
<scriptlanguage=javascript>
functionopenScript(url,width,height,left,top,r){
varWin=window.open(url,"openScript",'width='+width+',height='+height+',left='+left+',top='+top+',resizable=no,scrollbars='+r+',menubar=no,status=no');
}
//以下为禁止鼠标右键的代码,不想禁止的可以删除
<!--
if(window.Event)
document.captureEvents(Event.MOUSEUP);
functionnocontextmenu()
{
event.cancelBubble=true
event.returnValue=false;
returnfalse;
}
functionnorightclick(e)
{
if(window.Event)
{
if(e.which==2||e.which==3)
returnfalse;
}
else
{if(event.button==2||event.button==3){alert("【E路极速】欢迎你");}}
{
event.cancelBubble=true
event.returnValue=false;
returnfalse;
}
}
document.oncontextmenu=nocontextmenu;//forIE5+
document.onmousedown=norightclick;//forallothers
//-->
</script>
图片禁用鼠标右键代码
应用方法同上。
<scriptlanguage="JavaScript1.2">
varclickmessage="本站图片禁用右键!"
functiondisableclick(e){
if(document.all){
if(event.button==2||event.button==3){
if(event.srcElement.tagName=="IMG"){
alert(clickmessage);
returnfalse;
}
}
}
if(document.layers){
if(e.which==3){
alert(clickmessage);
returnfalse;
}
}
}
functionassociateimages(){
for(i=0;i<document.images.length;i++)
document.images[i].onmousedown=disableclick;
}
if(document.all)
document.onmousedown=disableclick
elseif(document.layers)
associateimages()
</script>
功能:禁止右键、禁选择、禁粘贴、禁shift、禁ctrl、禁alt
<scriptlanguage="JavaScript">
<!--
functionkey(){
if(event.shiftKey){
window.close();}
//禁止Shift
if(event.altKey){
window.close();}
//禁止Alt
if(event.ctrlKey){
window.close();}
//禁止Ctrl
returnfalse;}
document.onkeydown=key;
if(window.Event)
document.captureEvents(Event.MOUSEUP);
functionnocontextmenu(){
event.cancelBubble=true
event.returnValue=false;
returnfalse;}
functionnorightclick(e){
if(window.Event){
if(e.which==2||e.which==3)
returnfalse;}
else
if(event.button==2||event.button==3){
event.cancelBubble=true
event.returnValue=false;
returnfalse;}
}
//禁右键
document.oncontextmenu=nocontextmenu;//forIE5+
document.onmousedown=norightclick;//forallothers
//-->
</script>
<bodyonselectstart="returnfalse";onpaste="returnfalse";>
如何用用javascript禁止右键,禁止复制,禁止粘贴,做站时常会用到这些代码,所以收藏了一下!
1.oncontextmenu="window.event.returnValue=false"将彻底屏蔽鼠标右键特效
<tableborderoncontextmenu=return(false)><td>no</table>可用于Table
2.<bodyonselectstart="returnfalse">取消选取、防止复制javascript技巧
3.onpaste="returnfalse"不准粘贴技巧
4.oncopy="returnfalse;"oncut="returnfalse;"防止复制的javascirpt特效