How to Disable Right-Click Menu Using jQuery


This Article Is About How to Disable Right-Click Menu Using jQuery. There can be so many reasons of why you want to disable right click on any given website. A common one is that you don’t want your users to be able to steal or download your images (this can be done easily if your users have access to a right click menu, which gives users the option to save images when they right-click on them). Whether you want to protect your images or if you have another reason to want to do away with the right-click menu on your site, it’s actually really easy to disable this functionality with jQuery.

Disable Right-Click Menu Using jQuery

Here’s how you would do it using only a few simple lines of code:

$(document).bind(“contextmenu”, function(e) {

return false;

});

That’s all it takes, two simple lines of jQuery and the right click menu feature will be disabled.

Even though the code above will work for any of your projects, disabling right-click on an entire site or page isn’t really considered a coding best practice. If the reason you want to get rid of the right-click menu on your site is truly to protect your images, then the best way to do this is actually to disable the right-click menu only on your images, so that right click works everywhere on your pages except for if a user were to right-click on an image.

The code for this is also very simple and straightforward. In fact, it’s the same code snippet as the one above, only replacing the word “document” in the selector with “img”, so that all of the img tags are selected, rather than your entire page. Here’s how it looks:

$(‘img’).bind(“contextmenu”,function(e){

return false;

});

That code will help you to disable right-click on every image on your site. If you want to be more selective, assign the specific images you want to disable right-click for their own unique class, and then use that class name as the selector in place of the ‘img’ tag.


 

Leave a Reply

Your email address will not be published. Required fields are marked *