Click here to Skip to main content
Click here to Skip to main content

Disable Right Click On Web Page Using Javascript

, 24 Sep 2012
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Christopher John Paul
Software Developer
India India
I am a software developer with a passion to develop innovative ideas.
Follow on   Twitter   Google+

Comments and Discussions

 
GeneralGood Pinmemberrprabhuit4-Apr-14 23:02 
GeneralMy vote of 1 PinmemberNirav Prabtani 21-Jan-14 3:24 
GeneralMy vote of 1 PinprofessionalNirav Prabtani2-Jan-14 3:14 
GeneralMy vote of 3 Pinmembersandeepkumarvemula11-Feb-13 22:57 
GeneralMy vote of 1 Pinmemberr v25-Sep-12 7:23 
General[My vote of 1] Waste Pinmemberednrg25-Sep-12 3:16 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 25 Sep 2012
Article Copyright 2012 by Christopher John Paul
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid