JavaScript disabled right-click menu

Source: Internet
Author: User

In some cases, you do not need to right-click the page, for example, you do not want to view the source code of the page, or copy the content of the page. Of course, users can use various methods to achieve their goals, but we can try to increase the difficulty for them to achieve this goal. Use the following js script to implement this function:

<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> 

However, in some cases, the right-click of the input box cannot be blocked. You can use the following 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;")      }  }}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.