有时候,我们不希望自己网页上的精美图片或者文字被别人COPY,这时候就需要禁止鼠标右键的功能。笔者收集了几种不同的方法,现拿出来和各位网页DIYer分享。
方法一:最简单的方法
<SCRIPT language="JavaScript">
function click()
{
if(event.button==2)
{
alert('右键不能用了!')
}
}
document.onmousedown=click
</SCRIPT>
这是最简单,也是最常用的方法
方法二:添加到收藏夹
<SCRIPT language="JavaScript">
function click()
{
if(event.button==2)
{
window.external.addFavorite('http://td2shou.126.com','天地网络二手市场');
}
}
document.onmousedown=click
</SCRIPT>
方法三:使右键失效
将<body>改成<body oncontextmenu=self.event.returnValue=false>
该方法须IE5以上
方法四:
将<body>改成<body ondragstart="window.event.returnValue=false" oncontextmenu="window.event.returnValue=false" onselectstart="event.returnValue=false">
该方法使鼠标失效,效果和方法三差不多,但此时不能用鼠标选取页面上的内容,同时“编辑->全选”也没有作用。
方法五:定制自己的右键菜单
第1步:将下面的代码加到<head></head>中
<style>
#menu1
{
position:absolute;
width:140px;
border:1px dotted;
background-color:menu;
font-family:Arial;
line-height:20px;
cursor:default;
visibility:hidden;
}
.menu2
{
padding-left:10px;
padding-right:10px;
font-size:9pt;
font-family:Arial;
}
</style>
<script language="JavaScript">
function goto()
{
if(event.srcElement.className=="menu2")
{
if(event.srcElement.url=="reload")
window.location.reload();
else if(event.srcElement.url=="viewsource")
window.location="view-source:"+window.location.href;
else if(event.srcElement.url=="#")
return;
else
window.open(event.srcElement.url);
}
}
var showurl=1;
function show()
{
var rightedge=document.body.clientWidth-event.clientX;
var bottomedge=document.body.clientHeight-event.clientY;
if(rightedge<menu1.offsetWidth)
menu1.style.left=document.body.scrollLeft+event.clientX-menu1.offsetWidth;
else
menu1.style.left=document.body.scrollLeft+event.clientX;
if(bottomedge>menu1.offsetHeight)
menu1.style.top=document.body.scrollTop+event.clientY-menu1.offsetHeight;
else
menu1.style.top=document.body.scrollTop+event.clientY;
menu1.style.visibility="visible"
return false
}
function hide()
{
menu1.style.visibility="hidden";
}
function highlight()
{
if(event.srcElement.className=="menu2")
{
event.srcElement.style.backgroundColor="highlight";
event.srcElement.style.color="white";
if(showurl==1)
{
if(event.srcElement.url=="reload")
window.status="刷新页面";
else if(event.srcElement.url=="viewsource")
window.status="查看源文件";
else if(event.srcElement.url=="#")
return;
else
window.status=event.srcElement.url;
}
}
}
function lowlight()
{
if(event.srcElement.className=="menu2")
{
event.srcElement.style.backgroundColor="";
event.srcElement.style.color="black";
window.status=''
}
}
</script>
第2步:将下面的代码加到<body></body>中
<div id="menu1" onMouseover="highlight()" onMouseout="lowlight()" onClick="goto()">
<div class="menu2" url="#"><b>DV OnLine 中国DV数码在线网</b></div>
<hr width="98%">
<div class="menu2" url="http://www.dvol.cn"> 中国DV数码在线网</div>
<hr width="98%">
<div class="menu2" url="#"><b>常用链接</b></div>
<hr width="98%">
<div class="menu2" url="http://www.dvol.cn/url/"> 常用网址</div>
<div class="menu2" url="http://www.google.com"> Google</div>
<div class="menu2" url="http://www.dvol.cn/"> DVOL.cn</div>
<div class="menu2" url="http://www.baidu.com/"> baidu</div>
<div class="menu2" url="http://www.alexa.com/"> Alexa</div>
<hr width="98%">
<div class="menu2" url="reload"><b>刷新</b></div>
<div class="menu2" url="viewsource"><b>查看源文件</b></div>
</div>
<script language="JavaScript">
document.oncontextmenu=show;
if(document.all&&window.print)
document.body.onclick=hide;
</script>
其中前两种方法都能很容易地破解,后三种方法一般不易被破解,最Cool的当属第五种方法
(DVOL本文转自:中国DV传媒 http://www.dvol.cn)