[javascript] How to disable mouse right click on a web page?

I want to disable mouse right click on an HTML page. I have a page where user has to enter the details. I don't want the user to see the menu thats get displayed with the mouse right click. Rather I want to display a custom menu. I know there are some plugins available to do that. But my requirement does not need any plugins.

This question is related to javascript html mouse right-click

The answer is


window.oncontextmenu = function () {
return false;
}

might help you.


Please do not do that, it is very annoying.

The right menu is there for a reason, and it should be left there. Many browser extensions add entries to the right click menu and the user should be able to use it in any page he visits.

Moreover you can use all of the functionality of the right click menu in other ways (keyboard shortcuts, browser menu etc etc etc) so blocking the right click menu has the only effect of annoying the user.

PS: If really you cannot resist the urge to block it at least do not put a popup saying "no right click allowed".


You can use the oncontextmenu event for doing this.

But if the user turns off javascript then you won't be able to handle this.

window.oncontextmenu = function () {
   return false;
}

will disable right click menu.


//Disable right click script via java script code

<script language=JavaScript>
//Disable right click script
var message = "";
///////////////////////////////////
function clickIE() {
    if (document.all) {
        (message);
        return false;
    }
}

function clickNS(e) {
    if (document.layers || (document.getElementById && !document.all)) {
        if (e.which == 2 || e.which == 3) {
            (message);
            return false;
        }
    }
}
if (document.layers) {
    document.captureEvents(Event.MOUSEDOWN);
    document.onmousedown = clickNS;
} else {
    document.onmouseup = clickNS;
    document.oncontextmenu = clickIE;
}

document.oncontextmenu = new Function("return false")
</script>

Click here to see semo


You can do so with Javascript and/or an HTML attribute (which is really a Javascript event handler anyway) as described here: http://www.codeproject.com/KB/aspnet/Disabling_the_right_click.aspx

<script language="javascript">
document.onmousedown=disableclick;
status="Right Click Disabled";
Function disableclick(event)
{
  if(event.button==2)
   {
     alert(status);
     return false;    
   }
}
</script>

and

<body oncontextmenu="return false">
...
</body>

There are plenty of examples of this which can be found via Google

However, users can turn off Javascript to stop this highly annoying "feature". I think you should really think about this before implementing it. It isn't really going to protect your content (if that is what you are trying achieve).

There is an article here that illustrates just how annoying and pointless it is.


<body oncontextmenu="return false;"> works for me in Google Chrome. Not sure about other browsers.

Note, all someone has to do is disable JavaScript in order to see the right-click menu anyway.


Firstly, if you are doing this just to prevent people viewing the source of your page - it won't work, because they can always use a keyboard shortcut to view it.

Secondly, you will have to use JavaScript to accomplish this. If the user has JS disabled, you cannot prevent the right click.

That said, add this to your body tag to disable right clicks.

<body oncontextmenu="return false;">

Try this : write below code on body & feel the magic :)

body oncontextmenu="return false"

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to mouse

Determine which element the mouse pointer is on top of in JavaScript Change the mouse cursor on mouse over to anchor-like style Move the mouse pointer to a specific position? Copying text outside of Vim with set mouse=a enabled Java Mouse Event Right Click How to get mouse position in jQuery without mouse-events? Change background color on mouseover and remove it after mouseout How to disable mouse right click on a web page? Get Mouse Position Controlling mouse with Python

Examples related to right-click

Adding a right click menu to an item How to add a custom right-click menu to a webpage? Java Mouse Event Right Click Making custom right-click context menus for my web-app How to disable mouse right click on a web page? Right click to select a row in a Datagridview and show a menu to delete it right click context menu for datagridview How to distinguish between left and right mouse click with jQuery