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
»

7 Responsive jQuery Dropdown Menu Plugins

Dropdown Menu Plugins Dropdown menus are mostly used for website ...

article image
»

20 Stylish and Modern jQuery Tooltip Plugins

jQuery Tooltip Plugins Tooltips are a great option for showing extra ...

article image
»

15 Newest jQuery plugins for responsive website

Due to the proliferation of smartphones and tablets, responsive ...

article image
»

How to use Redux with jQuery

State management is one of most integral parts of any software and a ...

article image
»

Fade HTML Table Row on Hover Using jQuery

HTML tables are the ideal choice for displaying data in tabular form. ...

article image
»

Drag and Drop HTML Table Rows Using jQuery

HTML tables are the preferred UI option for displaying data. Sorting, ...

article image
»

Top 5 Best JavaScript Template Engines

Template engines help in the decoupling of HTML and JavaScript code ...

article image
»

3 jQuery/JavaScript Plugins for Push Notifications

Push notifications started from the mobile business but this ...

article image
»

Create Smooth Animation Using jQuery’s “stop()”

Making Quick Work jQuery’s animate() method is a quick way to ...