46

Is it possible to prevent RIGHT CLICK option for IMAGES which we use in web page.

apaul
  • 16,092
  • 8
  • 47
  • 82
mymotherland
  • 7,968
  • 14
  • 65
  • 122
  • 8
    I am normally annoyed by people questioning my motives instead of just answering the damn question, but do you really want to be hijacking a user's right mouse click? If they want to get your images they will get it one way or the other... Firebug or just looking at the source would be enough to get direct link to any of your images. – Daniel Sloof Apr 11 '11 at 07:32
  • 2
    Are you trying to prevent someone from downloading your images? In that case you can pretty much forget about it, because in order to seem the image in a browser is has to be copied to the client and will in most cases be stored somewhere on your disk. Also most browsers include a config option to prevent manipulation of context menues: [Example for Firefox](http://support.mozilla.com/media/uploads/gallery/images/668a9022f052f20f795759846994bc3f-1260050698-426-2.png). – nfechner Apr 11 '11 at 07:32
  • 2
    Might be worth mentioning WHY you are attempting to disable right click. If you think you are protecting your images, think again - you are only deterring the computer illiterates (which does have its merit). – Wesley Murch Apr 11 '11 at 07:33
  • As others have already pointed out here, sure it is. However, why? All someone has to do is disable Javascript, or view your page source to find the location, or use any number of more complex methods to get the image. – Demian Brecht Apr 11 '11 at 07:34
  • 2
    ...or the highly unorthodox and very complex "Print Screen" :) – Wesley Murch Apr 11 '11 at 07:41
  • Also, on mac, you can just drag the image off the page wherever you like. You might want to look at having the image not inside an tag and put it inside a
    as a css background to add further difficulty for the average user.
    – willdanceforfun Apr 11 '11 at 08:10
  • 3
    If protecting image is the reason a real solution would be to use watermark. Is it another X Y problem? – happy Mar 15 '14 at 03:03
  • 5
    I wish people would get off their high horses here, there are innumerable reasons for wanting to do this other than just stopping people downloading the image. If the OP wants to do it, then in the spirit of Q&A then if you know how answer the question. If the OP had said "so users can't download the image" after answering go on to tell them why it's not a complete solution. – Morvael Oct 06 '16 at 16:11
  • Most people do not even know how to bypass this block. – Black Nov 19 '17 at 09:37

15 Answers15

138
$(document).ready(function() {
    $("img").on("contextmenu",function(){
       return false;
    }); 
}); 

Working example: http://jsfiddle.net/vak9exyk/

Peeter
  • 9,282
  • 5
  • 36
  • 53
17

I think this should help. Trick is to bind the contextmenu event.

<script type="text/javascript" language="javascript">
        $(function() {
            $(this).bind("contextmenu", function(e) {
                e.preventDefault();
            });
        }); 
</script>
Fawzan
  • 4,738
  • 8
  • 41
  • 85
Abdul Kader
  • 5,781
  • 4
  • 22
  • 40
7

<body oncontextmenu="return false" onselectstart="return false" ondragstart="return false" >

Set these attributes in your selected tag

See here Working Example - https://codepen.io/Developer_Amit/pen/drYMMv

No Need JQuery (like)

Amit mishra
  • 347
  • 5
  • 15
6
$(document).ready(function() {

    $(document)[0].oncontextmenu = function() { return false; }

    $(document).mousedown(function(e) {
        if( e.button == 2 ) {
            alert('Sorry, this functionality is disabled!');
            return false;
        } else {
            return true;
        }
    });
});

If you want to disable it only on image click the instead of $(document).mousedown use $("#yourimage").mousedown

Robin Dorbell
  • 1,569
  • 1
  • 13
  • 26
Anupam
  • 7,966
  • 3
  • 41
  • 63
4

The following code will disable mouse right click from full page.

$(document).ready(function () {
   $("body").on("contextmenu",function(e){
     return false;
   });
});

The full tutorial and working demo can be found from here - Disable mouse right click using jQuery

JoyGuru
  • 1,803
  • 20
  • 11
4

Try this:

$(document).bind("contextmenu",function(e){
    return false;
});
daan.desmedt
  • 3,752
  • 1
  • 19
  • 33
Payal Mittal
  • 121
  • 2
  • 2
1

Here i have found some useful link, with live working example.

I have tried its working fine.

How to prevent Right Click option using jquery

$(document).bind("contextmenu", function (e) {
        e.preventDefault();
        alert("Right Click is Disabled");
    });
Barath Kumar
  • 439
  • 3
  • 7
  • For me, onmousedown event wasn't firing when hovering over an image on IE - I found that the only way to get an alert to appear was through this event. – chris.fy Nov 23 '16 at 11:30
  • Thank you. you can find some slimier article in the following link. http://www.thedevelopertips.com – Barath Kumar Mar 30 '19 at 07:08
1

Method 1:

<script type="text/javascript" language="javascript">
        $(document).ready(function(){

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

            return false;

            });

    });

</script>

Method 2:

<script type="text/javascript" language="javascript">
        $(document).ready(function(){

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

            e.preventDefault();

            });

    });

</script>
GIPSSTAR
  • 2,050
  • 1
  • 25
  • 20
1

Here is a working example, the red links can't be right clicked anymore.

$("ul.someLinks1 a").each(function(i, obj) {

  $(obj).on("contextmenu",function(){
     return false;
  }); 
  
  $(obj).css("color", "red");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="someLinks1">
  <li><a href="www.google.de">google</a></li>
  <li><a href="www.stackoverflow.de">stackoverflow</a></li>
  <li><a href="www.test.de">test</a></li>
</ul>

<ul class="someLinks2">
  <li><a href="www.foobar.de">foobar</a></li>
  <li><a href="www.foo.de">foo</a></li>
  <li><a href="www.bar.de">bar</a></li>
</ul>
Black
  • 18,150
  • 39
  • 158
  • 271
0
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
<script>
 $(document).ready(function(){
  $(document).bind("contextmenu",function(e){
  return false;
  });
});
</script>
</head>
<body>

<p>Right click is disabled on this page.</p>

</body>
</html>
Rohit Agrohia
  • 368
  • 3
  • 7
0

$(document).ready(function () {

        $("img").on('contextmenu', function (e) {
           e.preventDefault();
        });

});

0

You can also use the .contextmenu() shortcut method. For example:

$(document).ready(function() {
    $("#logo").contextmenu(function(e){
       return false;
    }); 
}); 
body {
    background-color: #FFF;
}
#logo {
    background: url(http://stackoverflow.com/favicon.ico) no-repeat; 
    width: 182px; 
    height: 30px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Try right-click on the logo.
<div id="logo"></div>
BaSsGaz
  • 666
  • 1
  • 18
  • 31
0

Try This!!

$('#element').mousedown(function(event) {
    switch (event.which) {
        case 3:
           $("img").on("contextmenu",function(){
       return false;
    }); 
            break;
    }
});
a.ak
  • 659
  • 2
  • 12
  • 26
Daniel
  • 1
-1
$(document).mousedown(function(e) {
    if( e.button == 2 ) {
         e.preventDefault();
        return false;
    } 
});
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
-3

If you're looking into trying to disable the downloading/saving of your images, scripts won't stop that. You would probably have better luck doing this on a server configuration level (like modifying your .htaccess for example on Apache).

Try asking this on ServerFault.

Richard Neil Ilagan
  • 14,627
  • 5
  • 48
  • 66