X

LATEST #JQUERY NEWS

How to Disable Right-Click Menu Using jQuery


There can be dozens of reasons why you may 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.

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 disable right-click on every image on your page. 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.

Recently Published

article image
»

How to Dynamically Trim Text Using jQuery

This easy beginners jQuery tutorial demonstrates how you can use ...

article image
»

jQuery Snippets: Getting the Current Year

Here’s a fun, simple, and incredibly useful jQuery that you can ...

article image
»

jQuery Snippets: How to Conditionally Enable Submit

One easy way to prevent users from submitting an empty form in a ...

article image
»

How to Use Google Analytics with jQuery Mobile

jQuery Mobile is an HTML5 based web-page framework that’s ...

article image
»

jQuery Snippets: Using jQuery to Check All Boxes

When you’ve given your users a ton of checkbox input fields ...

article image
»

Useful jQuery Code Snippets for Working with Images

Images are an essential part of any website and sometimes working ...

article image
»

How to Use jQuery to Check if Cookies are Enabled

A cookie (not the kind with chocolate chips) is a small piece of data ...

article image
»

A Beginner’s Guide to jQuery’s Selectors

We talk a lot about event methods on this site, but not so much about ...

article image
»

Add Keyboard Shortcut to Anchor Tag Using jQuery

Making a user friendly website can be quite a challenging task. There ...