Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Disable Right Click On Web Page Using Javascript

0.00/5 (No votes)
25 Sep 2012 1  
Prevent right click on the web page

Introduction

This tip will help you to prevent right click's on yourweb page... I have successfully implimented this code in one of our blog, That will give a demo.

Background

As a web developer you always want something like disabling mouse button right clicks... etc for some security issues. This simple javascript code would help you to implement that functionality. Major bank's net banking facilities are preventing users from right click for security reasons.

Using the code 

 For disabling the right click, first of all we need to identify which key is being clicked by the user.  We can identify the keys using the code e.buttons or event.button (in IE only). We first wire up the mouse click event to a specific function.

 document.onmousedown = clickfn;

In clickfn(e), the parameter e can be used only in bowsers other than IE 7 or earlier versions.  We can identify the buttons by the followinc code. Then we assign the button which is being clicked to a variable var button.

var button;
  if (navigator.appName == "Microsoft Internet Explorer") {
     button = event.button;// In IE, because IE 7 Doesnot support e.
               
     }
  else {
     button = e.buttons;// in other browsers
 }  

We can identify the browser using the Navigator object

navigator.appName == "Microsoft Internet Explorer"

navigator.appName will give the informations about the browser being used. 

  if (button == 2) {
    alert("Right Click Disabled");

    if (navigator.appName == "Microsoft Internet Explorer") {
         event.returnValue = false;
       }                
       return false;
   }

  event.returnValue = false; // IE 7 need this hack...

This code will disable right click with an alert message. i have successfully implemented in the following blog, check by right clicking on the blog page.

 For full code click here...

Please visit this blog for more info...

Points of Interest  

Javascript events and navigator object.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here