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.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)){
   return false;
 } else if (navigator.appName == 'Microsoft Internet Explorer'
       && (event.button == 2)) {
by Luigi Nori Date: 12-08-2019 javascript mouse snippet button hits : 3348  
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

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…

Dates in local format with Javascript

In the articles we have about dates in JavaScript we were missing one about how to create dates in local format with JavaScript. That is to say, being able to…

Formatting hours in Javascript

Continuing with the set of articles that talk about internationalisation elements, like the previous one where we talked about relative dates in JavaScript, we will see in this one how…

Request data with prompt in JavaScript

After having published several articles about how to manipulate arrays and dates, today I will publish a post that some of you will find too basic and others will find…

Relative dates in JavaScript

One of the interesting things about the internationalisation library represented in the Int object is the handling of relative dates in Javascript. This handling allows us to represent a date…

How to access webcam and grab an image using HTML5 and Javascript

We often use webcams to broadcast video in real time via our computer. This broadcast can be viewed, saved and even shared via the Internet. As a rule, we need…

The JavaScript forEach loop

We have already talked about how to handle some of loops  types in Javascript including for, for-in and for-off loops. In the case of today we are going to see how…

What are React Hooks and what problems they solve

Working with React, - and before the release of Hooks in version 16.8 -  there was always the possibility to create components in three different ways based on a number of…

Flattening arrays in JavaScript

When we are handling arrays that are arrays or have multiple dimensions it can be very useful to know how to flatten arrays in JavaScript. That is to say, to…

How to populate an array with random numbers in JavaScript

Some of you might think that what we explained in the article on how to populate an array with numbers, apart from the didactic part, would not have much applicability…

How to populate an array with numbers in JavaScript

Populate an array with numbers in JavaScript The first step is to initialise the array. So today we are going to see a simple way to do it and see how…

Top Javascript Libraries and Frameworks Part 2

What are JavaScript frameworks?   JavaScript frameworks are application frameworks that allow developers to manipulate code to meet their particular needs. Web application development is like building a house. You have the option…

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