46

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

10
  • 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. Commented Apr 11, 2011 at 7: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. Commented Apr 11, 2011 at 7: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). Commented Apr 11, 2011 at 7:33
  • 3
    If protecting image is the reason a real solution would be to use watermark. Is it another X Y problem? Commented Mar 15, 2014 at 3: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. Commented Oct 6, 2016 at 16:11

16 Answers 16

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

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

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

10 Comments

here's how i've extended it to add further difficulty for mac users jsfiddle.net/codedog/uKmDX
I honestly don't like the idea of preventing right-clicks on an image.
neither. we are now enablers of a worse interweb
Works perfect thanks. I am using this on a full screen browser in kiosk mode which I feel is a legit reason to disable to context menu.
@MartinSpamer Someone explained why they might need it. "I am using this on a full screen browser in kiosk mode which I feel is a legit reason to disable to context menu"... It's not promoting it either, it's answering a question. (Although I agree that disabling a right click is not advised)
|
18

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>

2 Comments

$(this) was what I was looking for, thanks! Also .bind() is depricated, use .on() instead
Thank you.$(this) solved my case as well. Great answer!
9

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

Comments

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

1 Comment

Thanks anu,like i said above... this prevent right click for whole page,i need only for image...
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

Comments

4

Try this:

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

Comments

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

2 Comments

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.
Thank you. you can find some slimier article in the following link. thedevelopertips.com
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>

Comments

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>

Comments

1
<!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>

Comments

1

$(document).ready(function () {

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

});

Comments

1

Try This!!

$('#element').mousedown(function(event) {
    switch (event.which) {
        case 3:
           $("img").on("contextmenu",function(){
       return false;
    }); 
            break;
    }
});

Comments

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>

1 Comment

The .contextmenu() method is now deprecated as of v3.3 (as per api.jquery.com/contextmenu-shorthand), but the similar .on("contextmenu", ...) works great.
-1
$(document).mousedown(function(e) {
    if( e.button == 2 ) {
         e.preventDefault();
        return false;
    } 
});

Comments

-1
     jQuery("#wpcf7-f608-p651-o1").on("contextmenu",function(e){
                    return false;
                }); 
                jQuery('.messagedata').keydown(function(event) {
                    if (event.ctrlKey==true && (event.which == '118' || event.which == '86')) {
                        //alert('thou. shalt. not. PASTE!');
                        event.preventDefault();
                     }
                });
                jQuery(document).on('keypress', '.messagedata', function (e) {
                    const pressedKey = e.key;
                    let pressedKeyCode = e.charCode;
                    
                    
                    if ((pressedKeyCode >= 33 && pressedKeyCode <= 47) || 
                        (pressedKeyCode >= 58 && pressedKeyCode <= 64) || 
                        (pressedKeyCode >= 91 && pressedKeyCode <= 96) || 
                        (pressedKeyCode >= 123 && pressedKeyCode <= 126) ) 
                    {
                        e.preventDefault();
                    
                        
                    }
                    else {
                        
                    }
                });
    jQuery('textarea').bind('input', function() {
  var c = this.selectionStart,
      r = /[^a-z0-9 .]/gi,
      v = jQuery(this).val();
  if(r.test(v)) {
    jQuery(this).val(v.replace(r, ''));
    c--;
  }
  this.setSelectionRange(c, c);
});

Comments

-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.

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.