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>
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.
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).
This works:
$('img').bind('contextmenu', function(e) { return false; });
Or for newer jQuery:
$('#nearestStaticContainer').on('contextmenu', 'img', function(e){ return false; });
jsFiddle example
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With