When I was trying to disable the right click, it is not working. I tried with below code.
document.onclick = function(e) {
console.log(e.button);
if(e.button == 2){
e.preventDefault();
return false;
}
}
Based on below link only I tried. Disable Right Click in website
I tested this code in Chrome and Firefox. Every time I am getting the right output only in console. I mean "0" for left click and "2" for right click.But still the default action is happening. May I know the reason.? Thanks in advance.
Only three lines of code can disable right click on a web page that is given below:
Source: Close current tab in browser window using JavaScript
You are using wrong event. For right click you should use
oncontextmenu
. You usedonclick
which obviously don't fire in right click and hence the loop doesn't evaluate to true in this case.Demo: http://jsfiddle.net/GCu2D/748/