JavaScript禁用滑鼠右鍵菜單

來源:互聯網
上載者:User

有些時候我們需要不讓使用者使用右鍵,比如不希望使用者查看本頁面原始碼,或者複製本頁的內容等等。當然使用者可以用各種方法來達到目的,但是我們可以盡量增加他們達到這個目的的難度。使用以下js指令碼就能夠實現這個功能:

<script type="text/javascript">// <![CDATA[function doProhibit(){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;  // }// ]]></script> 

但是有些時候,輸入框的右鍵不能屏蔽,那麼可以使用下面指令碼:

if (document.layers) {document.captureEvents(Event.MOUSEDOWN);}document.onmousedown = click;document.oncontextmenu = new Function("return false;")function click(e) {  e = e || event;  if (e.button == 2) {   var tag = e.srcElement || e.target;  if (tag.type == "text" || tag.type == "textarea")     {        document.oncontextmenu = new Function("return true;")     }      else       {     document.oncontextmenu = new Function("return false;")      }  }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.