Click here to Skip to main content
15,885,278 members
Articles / Web Development / HTML
Tip/Trick

Disable Right Click On Web Page Using Javascript

Rate me:
Please Sign up or sign in to vote.
1.50/5 (5 votes)
24 Sep 2012CPOL1 min read 37.5K   3   6
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 <code>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; <code>// 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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
I am a software developer with a passion to develop innovative ideas.

Comments and Discussions

 
GeneralGood Pin
rprabhuit4-Apr-14 23:02
rprabhuit4-Apr-14 23:02 
Nice. but we can also do this by http://www.prabhuraja.com/2014/04/04/disable-browsers-right-click-context-menu-using-jquery/[^]

GeneralMy vote of 1 Pin
Nirav Prabtani21-Jan-14 3:24
professionalNirav Prabtani21-Jan-14 3:24 
GeneralMy vote of 1 Pin
Nirav Prabtani2-Jan-14 3:14
professionalNirav Prabtani2-Jan-14 3:14 
GeneralMy vote of 3 Pin
sandeepkumarvemula11-Feb-13 22:57
sandeepkumarvemula11-Feb-13 22:57 
GeneralMy vote of 1 Pin
r v25-Sep-12 7:23
r v25-Sep-12 7:23 
General[My vote of 1] Waste Pin
ednrg25-Sep-12 3:16
ednrg25-Sep-12 3:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.