How to prevent Right Click option using jquery

113,576

Solution 1

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

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

Solution 2

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>

Solution 3

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

Solution 4

$(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

Solution 5

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

Share:
113,576

Related videos on Youtube

mymotherland
Author by

mymotherland

Eager to learn Live as if you were to die tomorrow. Learn as if you were to live forever! Let us continue learning as long as we live... only to add quality to the life we live! ஊக்கமது கைவிடேல் - எப்போதும் முயற்சியைக் கைவிடக்கூடாது.

Updated on June 23, 2021

Comments

  • mymotherland
    mymotherland almost 3 years

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

    • Daniel Sloof
      Daniel Sloof about 13 years
      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.
    • nfechner
      nfechner about 13 years
      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.
    • Wesley Murch
      Wesley Murch about 13 years
      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).
    • Demian Brecht
      Demian Brecht about 13 years
      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.
    • Wesley Murch
      Wesley Murch about 13 years
      ...or the highly unorthodox and very complex "Print Screen" :)
    • willdanceforfun
      willdanceforfun about 13 years
      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 <img> tag and put it inside a <div> as a css background to add further difficulty for the average user.
    • happy
      happy about 10 years
      If protecting image is the reason a real solution would be to use watermark. Is it another X Y problem?
    • Morvael
      Morvael over 7 years
      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.
    • Black
      Black over 6 years
      Most people do not even know how to bypass this block.
  • mymotherland
    mymotherland about 13 years
    Thanks anu,like i said above... this prevent right click for whole page,i need only for image...
  • willdanceforfun
    willdanceforfun about 13 years
    here's how i've extended it to add further difficulty for mac users jsfiddle.net/codedog/uKmDX
  • Peeter
    Peeter about 13 years
    I honestly don't like the idea of preventing right-clicks on an image.
  • willdanceforfun
    willdanceforfun about 13 years
    neither. we are now enablers of a worse interweb
  • willdanceforfun
    willdanceforfun about 13 years
    i just didn't know you could bind contextmenu :)
  • Richard Neil Ilagan
    Richard Neil Ilagan about 13 years
    Bad interweb design aside, this is one cool lesson for today. Didn't know you could do that. +1 :D
  • nspace
    nspace over 9 years
    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.
  • Richard de Wit
    Richard de Wit over 9 years
    @willdanceforfun - Your fiddle is broken. Also dont use a white image on a white background :P Updated fiddle. And OP: .bind() is depricated, use .on() instead
  • Richard de Wit
    Richard de Wit over 9 years
    $(this) was what I was looking for, thanks! Also .bind() is depricated, use .on() instead
  • Martin Spamer
    Martin Spamer about 9 years
    Downvoted for promoting web Browser Hijacking and standards breaking actions.
  • Peeter
    Peeter about 9 years
    Could you explain a bit @MartinSpamer ?
  • ʰᵈˑ
    ʰᵈˑ about 9 years
    @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)
  • Sannu
    Sannu about 9 years
    Thank you.$(this) solved my case as well. Great answer!
  • chris.fy
    chris.fy over 7 years
    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.
  • Sedat Kumcu
    Sedat Kumcu over 6 years
    Works fine. Thanks for this tip
  • Barath Kumar
    Barath Kumar about 5 years
    Thank you. you can find some slimier article in the following link. thedevelopertips.com