74

I want to know how to disable right click on images using jQuery.

I know only this:

<script type="text/javascript" language="javascript">
    $(document).ready(function() {
        $(document).bind("contextmenu",function(e) {
           return false;
        });
    }); 
</script>
6
  • 3
    this should work for sometimes ;). But forget it, there are 100 other ways to save an image from the web. By the way, if you are passing the clicked object, then use it! jsfiddle.net/VZX4A Commented Jan 20, 2011 at 23:11
  • 3
    yea i know. I'm doing this just to lower the number of image copies. It's specifically for those who only know to right click and save i.e. the dumb users. Commented Jan 20, 2011 at 23:33
  • 3
    Just don't. You're not protecting the image from being copied, and you're disabling default and expected functionality of the browser. Commented Jan 20, 2011 at 23:48
  • 5
    I am embedding a web browser inside an Access database. This browser contains a dynamic map. When you right click on the map there is a "save picture as..." option. When you save an image like that it will only save one tile of the map. This will confuse the hell out of the less-technically-minded people that I send this database to and I will no doubt have to explain. There is no reason at all for me to have that menu there. I agree that on websites disabling right-click is a bad thing but that doesn't mean you should never do it. Commented Mar 20, 2012 at 16:43
  • possible duplicate of How do I disable right click on my web page? Commented Apr 22, 2013 at 23:23

10 Answers 10

157

This works:

$('img').bind('contextmenu', function(e) {
    return false;
}); 

Or for newer jQuery:

$('#nearestStaticContainer').on('contextmenu', 'img', function(e){ 
  return false; 
});

jsFiddle example

Sign up to request clarification or add additional context in comments.

4 Comments

the new way is: $('body').on('contextmenu', 'img', function(e){ return false; }); although I'd recommend something narrower than 'body' if you can.
jQuery with the one-two knockout once again. <3
Hi Jacob, can you please tell me what are the lowest versions of respective browsers which support this jQuery feature?
thanks for you reply, but when the user press "shift"+ right Click , the context menu is displayed.
11

what is your purpose of disabling the right click. problem with any technique is that there is always a way to go around them. the console for firefox (firebug) and chrome allow for unbinding of that event. or if you want the image to be protected one could always just take a look at their temporary cache for the images.

If you want to create your own contextual menu the preventDefault is fine. Just pick your battles here. not even a big JavaScript library like tnyMCE works on all browsers... and that is not because it's not possible ;-).

$(document).bind("contextmenu",function(e){
  e.preventDefault()
});

Personally I'm more in for an open internet. Native browser behavior should not be hindered by the pages interactions. I am sure that other ways can be found to interact that are not the right click.

1 Comment

changed to better suit as clarification
8

For Disable Right Click Option

<script type="text/javascript">
    var message="Function Disabled!";

    function clickIE4(){
        if (event.button==2){
            alert(message);
            return false;
        }
    }

    function clickNS4(e){
        if (document.layers||document.getElementById&&!document.all){
            if (e.which==2||e.which==3){
                alert(message);
                return false;
            }
        }
    }

    if (document.layers){
        document.captureEvents(Event.MOUSEDOWN);
        document.onmousedown=clickNS4;
    }
    else if (document.all&&!document.getElementById){
        document.onmousedown=clickIE4;
    }

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

1 Comment

Props for the pure Javascript version, but the OP asked for jQuery.
7

In chrome and firefox the methods above didn't work unless I used 'live' instead of 'bind'.

This worked for me:

$('img').live('contextmenu', function(e){
    return false;
});

Comments

6

For modern browsers all you need is this CSS:

img {
    pointer-events: none;
}

Older browsers will still allow pointer events on the images, but the CSS above will take care of the vast majority of visitors to your site, and used in conjunction with the contextmenu methods should give you a very solid solution.

Comments

3

The better way of doing this without jQuery:

const images = document.getElementsByTagName('img');
for (let i = 0; i < images.length; i++) {
    images[i].addEventListener('contextmenu', event => event.preventDefault());
}

Comments

2

Would it be possible to leave the ability to right click and download just when done a separate watermark is placed on the image. Of course this won't prevent screen shots but thought it may be a good middle ground.

Comments

1

You could try this :

var message="Sorry, right-click has been disabled";

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")

Checkout a demo here

Comments

1

A very simple way is to add the image as a background to a DIV then load an empty transparent gif set to the same size as the DIV in the foreground. that keeps the less determined out. They cant get the background without viewing the code and copying the URL and right clicking just downloads the transparent gif.

Comments

1

This should work

$(function(){
     $('body').on('contextmenu', 'img', function(e){ 
         return false; 
     });
 });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.