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

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…

TypeScript: The evolution of JavaScript

When you're involved in the development of a large project, programming languages like JavaScript don't seem to be the best solution. Their lack of elements such as Language Aids has…

Awesome JavaScript Games and Js Software to create Games

Best sites js13kGames 2019 - Highlights from the js13kGames 2019 competition. js13kGames 2018 - 13 Games in ≤ 13kB of JavaScript. js13kGames 2017 - Build a game in 13kB or less with js13kGames. Adventure Triangle:…

Is JavaScript good for machine learning?

One of the things you always hear when you are talking to someone related to the M.L. world is that, one must learn Python because the vast majority of the…

First steps into JavaScript – a practical guide 3

After we learned the basic ofaccessing DOM elementsandhow to modify them,we are ready for the more exciting parts – handling DOM events. This allows us to make our web way more…

JavaScript: Promises explained with simple real life examples

Handling asynchronous data flows is complex, who hasn't faced codes like this one?: checkWeather('palma de mallorca', (error, weather) => { if (error) throw error; if (weather === 'well') { return checkFlights('palma…

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