How To Disable Right Button of Mouse in Web Pages

Disable the Right Button To prevent Users to show Web Page Code

by Luigi Nori Date: 12-08-2019 javascript mouse snippet button


In this snippet we are going to see how to block the right mouse button to prevent the user from taking out the context menu. This is good because in this context menu appears, among other options, to show source code.

Now that we can think that by deleting this context menu we prevent our source code from being inspected, although there are other ways that our source code can be stolen. However, there are many obstacles to our source code being inspected.

One of the worst things about the Internet is the discrepancies between different browsers. In part understandable by the slowness in the standardization of the committees (slowness compared to the speed of development of the network).

Although DOM Level2 Events tries to standardize event control. It is not possible today to make a single script for different browsers. And in all this embolado, Opera does not offer the support of cancellation of events and promotes the use of the contextual menu obtained by the right button. A mess.

If one thing is still more or less clear, although the subject press and release a button we could make a monologue, is that events can be captured with onmousedown. However, this may not prevent the browser from launching the context menu. It's not too much to stop to the context menu with the oncontextmenu attribute. To which, we only need to give a false value.

 

  document.onmousedown=anularBotonDerecho;  
  document.oncontextmenu=new Function("return false");

When calculating the pressed button we have two alternatives. Internet Explorer browsers are still based on the property event.button and the value 2 (right button).

  if (event.button == 2) {  
   alert("Right Button Clicked");
   }

For its part, Firefox and Chrome rely on the property which and its values 2 and 3.

  if (e.which == 3 || e.which == 2) {  
  alert("Right Button Clicked");
  }

In addition to validating the button pressed, we will add in the validation the browser on which we operate. In this case we will rely on the property Navigator.appName

function stopRightButton(e) {
 if (navigator.appName == 'Netscape' 
       && (e.which == 3 || e.which == 2)){
   alert(sMessage);
   return false;
 } else if (navigator.appName == 'Microsoft Internet Explorer'
       && (event.button == 2)) {
   alert(sMessage);
 }
}
 
by Luigi Nori Date: 12-08-2019 javascript mouse snippet button hits : 3553  
 
Luigi Nori

Luigi Nori

He has been working on the Internet since 1994 (practically a mummy), specializing in Web technologies makes his customers happy by juggling large scale and high availability applications, php and js frameworks, web design, data exchange, security, e-commerce, database and server administration, ethical hacking. He happily lives with @salvietta150x40, in his (little) free time he tries to tame a little wild dwarf with a passion for stars.

 
 
 

Related Posts

Difference between arrow and normal functions in JavaScript

In this tutorial we are going to see how arrow functions differ from normal JavaScript functions. We will also see when you should use one and when you should use…

JavaScript Arrow functions: What they are and how to use them

In this article we are going to see what they are and how to use JavaScript Arrow Functions, a new feature introduced with the ES6 standard (ECMAScript 6). What are Arrow…

How to insert an element into an array with JavaScript

In this brief tutorial you will learn how to insert one or more elements into an array with JavaScript. For this we will use the splice function. The splice function will not…

What is the difference between primitives types and objects in JavaScript?

In this short tutorial we are going to look at the differences between primitive types and objects in JavaScript. To start with, we're going to look at what primitive types…

How to get DOM elements with JavaScript

When you access any element of the DOM, it is usual to save it in a variable. This is something that at first might seem very simple, but if you…

How to reverse an array in JavaScript

In this tutorial we are going to see how you can change the order of the elements of an array so that they are inverted. You could use a loop…

How synchronize the scroll of two divs with JavaScript

In case you have two divs of different sizes you may sometimes want to scroll both at the same time but at different speeds depending on their size. For example,…

How to use the codePointAt method in JavaScript

The JavaScript codePointAt method has more or less the same function as the charCodeAt method, used to get the 16-bit Unicode representation of the character at a certain position in…

How to check if a value is a number in JavaScript

In this short tutorial we are going to look at the various methods that exist to find out if a value is a number in JavaScript.   1. Using the isNaN() function   One…

How to use the charCodeAt method in JavaScript

The charCodeAt method is accepted by strings in JavaScript, returning the 16-bit Unicode code of the character at the position we pass as a parameter to the method. The charCodeAt method…

How to use the charAt method in JavaScript

The charAt method is accepted by strings in JavaScript, returning the position of the character passed as a parameter within the string. If the string contains multiple occurrences of the character…

Strings in JavaScript: What they are and how to use them

In this tutorial we are going to explain what strings are and how they are used in JavaScript. The tutorial is intended for people who are learning to program in…

We use our own and third-party cookies to improve our services, compile statistical information and analyze your browsing habits. This allows us to personalize the content we offer and to show you advertisements related to your preferences. By clicking "Accept all" you agree to the storage of cookies on your device to improve website navigation, analyse traffic and assist our marketing activities. You can also select "System Cookies Only" to accept only the cookies required for the website to function, or you can select the cookies you wish to activate by clicking on "settings".

Accept All Only sistem cookies Configuration