73

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>
user3708642
  • 124
  • 10
Bat_Programmer
  • 6,717
  • 10
  • 56
  • 67
  • 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! http://jsfiddle.net/VZX4A/ – meo Jan 20 '11 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. – Bat_Programmer Jan 20 '11 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. – Joe Jordan Jan 20 '11 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. – Mr_Chimp Mar 20 '12 at 16:43
  • possible duplicate of [How do I disable right click on my web page?](http://stackoverflow.com/questions/737022/how-do-i-disable-right-click-on-my-web-page) – Cole Tobin Apr 22 '13 at 23:23
  • try this plugin https://github.com/thatisuday/copynote – Uday Hiwarale Apr 24 '15 at 07:54

10 Answers10

155

This works:

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

Or for newer jQuery:

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

jsFiddle example

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
  • 53
    the new way is: `$('body').on('contextmenu', 'img', function(e){ return false; }); ` although I'd recommend something narrower than 'body' if you can. – Myster May 21 '12 at 04:31
  • 1
    jQuery with the one-two knockout once again. <3 – Foxinni Sep 13 '12 at 15:44
  • 2
    Hi Jacob, can you please tell me what are the lowest versions of respective browsers which support this jQuery feature? – SexyBeast Jan 26 '13 at 12:07
  • 1
    thanks for you reply, but when the user press "shift"+ right Click , the context menu is displayed. – Sara N May 21 '15 at 05:57
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.

yopefonic
  • 524
  • 2
  • 10
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>
MaxPRafferty
  • 4,819
  • 4
  • 32
  • 39
Prince Antony G
  • 932
  • 4
  • 18
  • 39
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;
});
antyrat
  • 27,479
  • 9
  • 75
  • 76
Emz
  • 71
  • 1
  • 2
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.

Gavin
  • 7,544
  • 4
  • 52
  • 72
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());
}
Amir Hassan Azimi
  • 9,180
  • 5
  • 32
  • 43
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.

EZDC
  • 682
  • 1
  • 17
  • 33
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

Pranav 웃
  • 8,469
  • 6
  • 38
  • 48
Serkan
  • 11
  • 1
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.

Chris
  • 11
  • 1
1

This should work

$(function(){
     $('body').on('contextmenu', 'img', function(e){ 
         return false; 
     });
 });
Mohamad Hamouday
  • 2,070
  • 23
  • 20