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

Screen Shot 2017-04-23 at 10.15.41 AM
»

How to Use jQuery to Create a Sticky Navigation on Scroll

Creating “sticky” elements is something that can often be ...

article image
»

How to Use jQuery’s Keystroke Methods

jQuery has a few built-in keystroke methods that are all triggered by ...

article image
»

How to Change iFrame Styling Using jQuery

If you’ve ever had the displeasure of working with iFrames, ...

article image
»

How to Use jQuery to Create Blinking Text

Blinking text is one of those cool features that you never actively ...

article image
»

Convert Images to Black and White using jQuery

Converting an image to black and white is no longer a manual event. ...

article image
»

How to Disable Right-Click Menu Using jQuery

There can be dozens of reasons why you may want to disable right ...

article image
»

How to Optimize jQuery Code for Better Performance

jQuery is the most popular client side library, in fact almost every ...

article image
»

Using Head.js to Load All Your JavaScript

In the past few years, client side frameworks/libraries have become ...

article image
»

How to Detect Browser Using jQuery

One of the more frustrating parts of creating a new web project is ...