JavaScript - Simple Disable Right Click
Submitted by razormist on Sunday, May 19, 2019 - 19:36.
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 .
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!
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.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- </div>
- </nav>
- <div class="col-md-6 well">
- <hr style="border-top:1px dotted #ccc;"/>
- <br />
- <br />
- </div>
- </body>
- </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.- var message="The right click mouse button has been disable by the Administrator";
- function mouseBtn(){
- if(document.all){
- alert(message);
- return false;
- }
- }
- function disableBtn(e){
- if(document.layers||(document.getElementById&&!document.all)){
- if (e.which==2||e.which==3){
- alert(message);
- return false;
- }
- }
- }
- if (document.layers){
- document.captureEvents(Event.MOUSEDOWN);
- document.onmousedown=disableBtn;
- }else{
- document.onmouseup=disableBtn;document.oncontextmenu=mouseBtn;
- }
- document.oncontextmenu=new Function("return false");
Add new comment
- 191 views