Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I disable right click on my web page?

Tags:

javascript

Can I disable right click on my web page without using JavaScript? I ask this because most browsers allow user to disable JavaScript.

If not, how do I use JavaScript to disable right click?

like image 200
Vinay Pandey Avatar asked Apr 10 '09 08:04

Vinay Pandey


People also ask

How do I disable right click on a web page?

Disable right click menu in html page using jquery. JavaScript Code: $(document). bind("contextmenu",function(e){ return false; });

Should I disable right click on my website?

It is pointlessIt does not protect your source code, images, or content. Disabling right-click and Ctrl+U will get users curious, wanting to know what it is exactly that you want to hide. This will end up being counter-productive to you as your images and source code will attract the attention of the tech survey.

How do I disable right click on Chrome?

Method 2: Disable JavaScript in Web Browser Be that as it may, here's how you can turn off JavaScript in Chrome, Firefox, and Edge. On Chrome, go to 'Settings -> Privacy and Security -> Site Settings -> JavaScript' and then switch off the toggle.


2 Answers

You can do that with JavaScript by adding an event listener for the "contextmenu" event and calling the preventDefault() method:

document.addEventListener('contextmenu', event => event.preventDefault()); 

That being said: DON'T DO IT.

Why? Because it achieves nothing other than annoying users. Also many browsers have a security option to disallow disabling of the right click (context) menu anyway.

Not sure why you'd want to. If it's out of some misplaced belief that you can protect your source code or images that way, think again: you can't.

like image 56
cletus Avatar answered Sep 18 '22 19:09

cletus


DON'T

Just, don't.

No matter what you do, you can't prevent users from having full access to every bit of data on your website. Any Javascript you code can be rendered moot by simply turning off Javascript on the browser (or using a plugin like NoScript). Additionally, there's no way to disable the ability of any user to simply "view source" or "view page info" (or use wget) for your site.

It's not worth the effort. It won't actually work. It will make your site actively hostile to users. They will notice this and stop visiting. There is no benefit to doing this, only wasted effort and lost traffic.

Don't.

Update: It seems this little topic has proven quite controversial over time. Even so, I stand by this answer to this question. Sometimes the correct answer is advice instead of a literal response.

People who stumble on this question in hopes of finding out how to create custom context menus should look elsewhere, such as these questions:

  • Making custom right-click context menus for my web-app, which relies on jQuery
  • How to add a custom right-click menu to a webpage, which uses pure javascript/html
like image 28
Wedge Avatar answered Sep 21 '22 19:09

Wedge