JavaScript suppresses the right mouse click instance Code

Source: Internet
Author: User


Look at the code:

The code is as follows Copy Code


<body oncontextmenu= "alert (' ^_^ Don't peek Oh! '); return false >
Right on top of me, look.
</body>


Right-click on the top of the text, pop-up message box, as shown:

The same function, can be implemented in many ways, it depends on your personal practice, the above functions can also be achieved through the following code:

The code is as follows Copy Code

<script language= "JavaScript" >
<!--
function Getbutton () {
if (Event.button = = 2) {
Alert ("^_^ Don't peek!" ");
}
}
Document.onmousedown = Getbutton;
-->
</script>
<body>
Disable right mouse button
</body>


But sometimes, the right key of the input box is not blocked, then you can use the following script:

The code is as follows Copy Code

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)
 {
&NBSP;&N Bsp   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;")
       }
  &NBSP;}
}

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.