How do I disable right click on my web page?

How do I disable right click on my web page?

Can I disable right click on my web page without using JavaScript? I ask this because most browsers allow user to disable JavaScript.
If not, how do I use JavaScript to disable right click?


Solution 1:

You can do that with JavaScript by adding an event listener for the “contextmenu” event and calling the preventDefault() method:

document.addEventListener('contextmenu', event => event.preventDefault());

That being said: DON’T DO IT.

Why? Because it achieves nothing other than annoying users. Also many browsers have a security option to disallow disabling of the right click (context) menu anyway.

Not sure why you’d want to. If it’s out of some misplaced belief that you can protect your source code or images that way, think again: you can’t.

Solution 2:


Just, don’t.

No matter what you do, you can’t prevent users from having full access to every bit of data on your website. Any Javascript you code can be rendered moot by simply turning off Javascript on the browser (or using a plugin like NoScript). Additionally, there’s no way to disable the ability of any user to simply “view source” or “view page info” (or use wget) for your site.

It’s not worth the effort. It won’t actually work. It will make your site actively hostile to users. They will notice this and stop visiting. There is no benefit to doing this, only wasted effort and lost traffic.


Update: It seems this little topic has proven quite controversial over time. Even so, I stand by this answer to this question. Sometimes the correct answer is advice instead of a literal response.

People who stumble on this question in hopes of finding out how to create custom context menus should look elsewhere, such as these questions:

Solution 3:

The original question was about how to stop right-click given that the user can disable JavaScript: which sound nefarious and evil (hence the negative responses) – but all duplicates redirect here, even though many of the duplicates are asking for less evil purposes.

Like using the right-click button in HTML5 games, for example. This can be done with the inline code above, or a bit nicer is something like this:

document.addEventListener("contextmenu", function(e){
}, false);

But if you are making a game, then remember that the right-click button fires the contextmenu event – but it also fires the regular mousedown and mouseup events too. So you need to check the event’s which property to see if it was the left (which === 1), middle (which === 2), or right (which === 3) mouse button that is firing the event.

Here’s an example in jQuery – note that the pressing the right mouse button will fire three events: the mousedown event, the contextmenu event, and the mouseup event.

// With jQuery
    "contextmenu": function(e) {
        console.log("ctx menu button:", e.which); 

        // Stop the context menu
    "mousedown": function(e) { 
        console.log("normal mouse down:", e.which); 
    "mouseup": function(e) { 
        console.log("normal mouse up:", e.which); 

So if you’re using the left and right mouse buttons in a game, you’ll have to do some conditional logic in the mouse handlers.

Solution 4:

If you don’t care about alerting the user with a message every time they try to right click, try adding this to your body tag

<body oncontextmenu="return false;">

This will block all access to the context menu (not just from the right mouse button but from the keyboard as well).

However, as mentioned in the other answers, there really is no point adding a right click disabler. Anyone with basic browser knowledge can view the source and extract the information they need.

Solution 5:

If you are a jquery fan,use this

    $(function() {
        $(this).bind("contextmenu", function(e) {

Solution 6:

If your aim is to prevent people being able to download your images, as most people have said, disabling right click is pretty much ineffective.

Assuming you are trying to protect images the alternative methods are –

Using a flash player, users can’t download them as such, but they could easily do a screen capture.

If you want to be more akward, make the image the background of a div, containing a transparent image, à la –

<div style="background-image: url(YourImage.jpg);">
   <img src="transparent.gif"/>

will be enough to deter the casual theft of your images (see below for a sample), but as with all these techniques, is trivial to defeat with a basic understanding of html.