How to disable mouse right click on a web page?
Solution 1
It's unprofessional, anyway this will work with javascript enabled:
document.oncontextmenu = document.body.oncontextmenu = function() {return false;}
You may also want to show a message to the user before returning false.
However I have to say that this should not be done generally because it limits users options without resolving the issue (in fact the context menu can be very easily enabled again.).
The following article better explains why this should not be done and what other actions can be taken to solve common related issues: http://articles.sitepoint.com/article/dont-disable-right-click
Solution 2
Firstly, if you are doing this just to prevent people viewing the source of your page - it won't work, because they can always use a keyboard shortcut to view it.
Secondly, you will have to use JavaScript to accomplish this. If the user has JS disabled, you cannot prevent the right click.
That said, add this to your body tag to disable right clicks.
<body oncontextmenu="return false;">
Solution 3
You can use the oncontextmenu event for doing this.
But if the user turns off javascript then you won't be able to handle this.
window.oncontextmenu = function () {
return false;
}
will disable right click menu.
Solution 4
<body oncontextmenu="return false;">
works for me in Google Chrome. Not sure about other browsers.
Note, all someone has to do is disable JavaScript in order to see the right-click menu anyway.
Solution 5
There are plenty of examples of this which can be found via Google
However, users can turn off Javascript to stop this highly annoying "feature". I think you should really think about this before implementing it. It isn't really going to protect your content (if that is what you are trying achieve).
There is an article here that illustrates just how annoying and pointless it is.
Related videos on Youtube
Puru
Updated on July 09, 2022Comments
-
Puru almost 2 years
I want to disable mouse right click on an HTML page. I have a page where user has to enter the details. I don't want the user to see the menu thats get displayed with the mouse right click. Rather I want to display a custom menu. I know there are some plugins available to do that. But my requirement does not need any plugins.
-
Felix Kling almost 14 yearsWhat is the problem with the menu? It belongs to the user so leave it to him. Just imagine, a user does not understand a word and wants to use some translation service that is accessible vie the context menu.
-
Henrik Gering almost 14 yearsWhy do you want to do this? Many users find it confusing if the page does not work as all other webpages.
-
Puru almost 14 yearsMy requirement is that the user shouldn't copy anything. I disabled through the Key board. I tried with the mouse. I couldn't able to do that. Thats why I posted the question here.
-
Felix Kling almost 14 years@Purushotham: You cannot prevent this.
-
cHao almost 14 yearsAny file that can be seen, heard, or run, by someone can be copied by that same person. You can't prevent it, and in the case of web pages and images, you can't even make it difficult -- the very act of viewing your page creates a local copy of it on the user's computer, in almost all browsers.
-
Tom Gullen over 13 yearsPlease don't implement this. It's so incredibly annoying.
-
selfawaresoup over 13 yearsIf someone wants to copy your content, he/she WILL succeed. There's nothing you can do about that. Waht you're trying will just piss off the "good" visitors of your website.
-
Bobby over 12 yearsI am writing a web app for students (grade 3-12) to learn about science. In a field test, I observed that the third graders testing it kept accidentally right clicking and bringing up the context menu. It distracted them from the content. So for all of you who think that there zero good reasons for anyone to do this, and down voted the question, you are wrong.
-
NoBugs about 11 yearsWhy not give them some old Mac mice :)
-
-
Quentin almost 14 years-1: While this makes some good points, it doesn't answer the question so should be a comment.
-
nico almost 14 years@David Dorward: It does indeed answer the question. I'm saying that although he can block the appearance of the right click menu there is NO way to actually block its functionality, as they are reacheable in other ways. Blocking right click menu is bad practice, so I found it a much better answer to explain why he should not.
-
Dejan over 13 yearsFurthermore, this isn't a be all and end all solution, they could just disable js and hey presto.
-
Jerry Huckins over 6 yearsWhy not just
document.oncontextmenu = function () { return false; }
?