Click here to Skip to main content
13,251,998 members (48,178 online)
Click here to Skip to main content
Add your own
alternative version


1 bookmarked
Posted 25 Sep 2012

Disable Right Click On Web Page Using Javascript

, 25 Sep 2012
Rate this:
Please Sign up or sign in to vote.
Prevent right click on the web page


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.


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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Christopher John Paul
Software Developer
India India
I am a software developer with a passion to develop innovative ideas.

You may also be interested in...


Comments and Discussions

General[My vote of 1] Waste Pin
ednrg25-Sep-12 4:16
memberednrg25-Sep-12 4: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 25 Sep 2012
Article Copyright 2012 by Christopher John Paul
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid