[jquery] How to prevent Right Click option using jquery

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

This question is related to jquery

The answer is


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

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


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


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>

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


$(document).ready(function () {

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

});


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.


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

$(document).mousedown(function(e) {
    if( e.button == 2 ) {
         e.preventDefault();
        return false;
    } 
});

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>

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

Try this:

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

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