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 : 3073  
 
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

Creating simple CSS spinner-loader

In today's article we will show you how to animate a basic loader that spins when some predefined action is defined, such as loading an image. That can be used…

Validating HTML forms using BULMA and vanilla JavaScript

Today we are going to write about contact forms and how to validate them using JavaScript. The contact form seems to be one of the top features of every basic home…

A FULFILLED PROMISE - Using the FETCH API to make AJAX calls

In this article we talked about what AJAX calls are and how to use them in a traditional way, by using the XMLHttpRequest (XHR) object. In short, thanks to AJAX…

How to use Parallax.js effect on your website

Today, we're going to write about the parallax effect, similar to parallax scrolling, and how to implement it to improve your landing page. In webdev, they say mobile first -…

How to make the website's dark mode persistent with Local Storage, CSS and JS

Recently we wrote about how to do a switchable alternative color mode or theme, a very useful and popular feature to websites. Today’s article is going to be about how…

Dark Mode on website using CSS and JavaScript

In today’s article we are going to learn how to build pretty much standard these days on the web pages and that is the alternative color mode and switching between…

JavaScript: Spread and Rest operators

In today’s article we are going to talk about one of the features of the ES6 version(ECMAScript 2015) of JavaScript which is Spread operator as well as Rest operator. These features…

Javascript: what are callbacks and how to use them.

Today we are going to learn about a concept that is widely used in javascript and that is used quite a lot by today's frameworks, libraries, especially NodeJS. This is…

HTTP Cookies: how they work and how to use them

Today we are going to write about the way to store data in a browser, why websites use cookies and how they work in detail. Continue reading to find out how…

The package managers npm and yarn: main differences

Npm and yarn are package managers that help to manage a project’s dependencies. A dependency is, as it sounds, something that a project depends on, a piece of code that…

The Javascript asign() method to merge and clone objects

In this article, we will be covering Object.assign()method in javascript in detail with examples. A javascript object is a collection of key-value pairs. Keys are also known as properties of object. Keys…

All the javascript functions and methods to manipulate arrays

This article will show that the prominent JavaScript array functions are .map(), .filter(), and .reduce(), and will then go through examples of instances in which .every() and .some() would save…

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