国内免费空间一览,(网络整理)- -| 回首页 | 2006年索引 | - -精彩:JavaScript经典技巧

禁止鼠标右键的几种方法

                                      

有时候,我们不希望自己网页上的精美图片或者文字被别人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>我的网站</b></div> 
<hr width="98%">
<div class="menu2" url="http://td2shou.126.com">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;天地网络二手市场</div> 
<hr width="98%">
<div class="menu2" url="#"><b>常用链接</b></div> 
<hr width="98%">
<div class="menu2" url="http://www.hotmail.com">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HotMail</div> 
<div class="menu2" url="http://www.google.com">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Google</div> 
<div class="menu2" url="http://www.onlinedown.net">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;华军软件园</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>

【作者: 飞鸿】【访问统计:】【2006年05月23日 星期二 10:21】【 加入博采】【打印

Trackback

你可以使用这个链接引用该篇文章 http://publishblog.blogchina.com/blog/tb.b?diaryID=5098094

博客手拉手

回复

验证码:   
评论内容: