JavaScript - Simple Disable Right Click

In this tutorial we will create a Simple Disable Right Click using JavaScript. This code will automatically disable the user right click mouse button when applied. The code use a simple onmousedown to track when the button has been click and by adding captureEvent you can detect wether the button you click is exactly the right button. This is a free program, you can modify it and use it as your own. We will be using JavaScript as a server-side scripting language because It allows greater control of your web page behavior than HTML alone. It is embedded in HTML that responsible to allow user to interact with the computer .

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  4.         <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  5. </head>
  6.         <nav class="navbar navbar-default">
  7.                 <div class="container-fluid">
  8.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  9.                 </div>
  10.         </nav>
  11.         <div class="col-md-3"></div>
  12.         <div class="col-md-6 well">
  13.                 <h3 class="text-primary">JavaScript - Simple Disable Right Click</h3>
  14.                 <hr style="border-top:1px dotted #ccc;"/>
  15.                 <br />
  16.                 <center><h1>Press the right click mouse button to try if it is disabled</h1></center>
  17.                 <br />
  18.         </div>
  19.        
  20. <script src="js/script.js"></script>
  21. </body>
  22. </html>

Creating the Script

This code contains the script of the application. This code will prevent the user by clicking the right click when applied. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. var message="The right click mouse button has been disable by the Administrator";
  2.  
  3. function mouseBtn(){
  4.         if(document.all){
  5.                 alert(message);
  6.                 return false;
  7.         }
  8. }
  9.  
  10. function disableBtn(e){
  11.         if(document.layers||(document.getElementById&&!document.all)){
  12.                 if (e.which==2||e.which==3){
  13.                         alert(message);
  14.                         return false;
  15.                 }
  16.         }
  17. }
  18.  
  19. if (document.layers){
  20.         document.captureEvents(Event.MOUSEDOWN);
  21.         document.onmousedown=disableBtn;
  22. }else{
  23.         document.onmouseup=disableBtn;document.oncontextmenu=mouseBtn;
  24. }
  25.  
  26. document.oncontextmenu=new Function("return false");
There you have it we successfully created a Simple Disable Right Click using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment