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;
}
else {
button = e.buttons; }
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.