Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling right click on images using jquery

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> 
like image 875
Bat_Programmer Avatar asked Jan 20 '11 23:01

Bat_Programmer


People also ask

How do you disable right click on images?

Disabling right-clicksOpen the section General settings. At the top of this section you can choose any of the following options in the dropdown box: A) Don't disable right-click on images.

How do you disable right click on images using JavaScript?

Whenever a user tries to right-click on the image (i.e Event Occurs), function(e) is called which in turn calls the inbuilt function preventDefault() which prevents the default event to occur(here right-click is prevented).


Video Answer


2 Answers

This works:

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

Or for newer jQuery:

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

jsFiddle example

like image 103
Jacob Relkin Avatar answered Sep 29 '22 10:09

Jacob Relkin


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.

like image 33
yopefonic Avatar answered Sep 29 '22 10:09

yopefonic