Skip to content Skip to sidebar Skip to footer

Disable Right Click In Javascript

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.preve

Solution 1:

You are using wrong event. For right click you should use oncontextmenu. You used onclick which obviously don't fire in right click and hence the loop doesn't evaluate to true in this case.

document.oncontextmenu = function (e) {
    console.log(e.button);
    if (e.button == 2) {
        e.preventDefault();
        return false;
    }

}

Demo: http://jsfiddle.net/GCu2D/748/


Solution 2:

Only three lines of code can disable right click on a web page that is given below:

$("html").on("contextmenu",function(e){
   return false;
});

Source: Close current tab in browser window using JavaScript


Post a Comment for "Disable Right Click In Javascript"