22

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.

Puru
  • 8,913
  • 26
  • 70
  • 91
  • 15
    What 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.* – Felix Kling Jun 21 '10 at 10:27
  • 8
    Why do you want to do this? Many users find it confusing if the page does not work as all other webpages. – Henrik Gering Jun 21 '10 at 10:27
  • My 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. – Puru Jun 21 '10 at 12:19
  • 1
    @Purushotham: You cannot prevent this. – Felix Kling Jun 21 '10 at 12:22
  • 2
    Any 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. – cHao Jun 21 '10 at 18:29
  • 3
    Please don't implement this. It's so incredibly annoying. – Tom Gullen Aug 09 '10 at 10:59
  • 2
    If 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. – selfawaresoup Aug 10 '10 at 09:49
  • 3
    I 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. – Bobby Aug 23 '11 at 22:08
  • 2
    Why not give them some old Mac mice :) – NoBugs Apr 19 '13 at 03:59

10 Answers10

51

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

Andrea Zilio
  • 4,444
  • 3
  • 29
  • 34
11

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;">
romiem
  • 8,360
  • 7
  • 29
  • 36
6

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.

rahul
  • 184,426
  • 49
  • 232
  • 263
6

<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.

cHao
  • 84,970
  • 20
  • 145
  • 172
3

Please do not do that, it is very annoying.

The right menu is there for a reason, and it should be left there. Many browser extensions add entries to the right click menu and the user should be able to use it in any page he visits.

Moreover you can use all of the functionality of the right click menu in other ways (keyboard shortcuts, browser menu etc etc etc) so blocking the right click menu has the only effect of annoying the user.

PS: If really you cannot resist the urge to block it at least do not put a popup saying "no right click allowed".

nico
  • 50,859
  • 17
  • 87
  • 112
  • 3
    -1: While this makes some good points, it doesn't answer the question so should be a comment. – Quentin Jun 21 '10 at 10:53
  • @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. – nico Jun 21 '10 at 13:36
3

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.

codingbadger
  • 42,678
  • 13
  • 95
  • 110
2

You can do so with Javascript and/or an HTML attribute (which is really a Javascript event handler anyway) as described here: http://www.codeproject.com/KB/aspnet/Disabling_the_right_click.aspx

<script language="javascript">
document.onmousedown=disableclick;
status="Right Click Disabled";
Function disableclick(event)
{
  if(event.button==2)
   {
     alert(status);
     return false;    
   }
}
</script>

and

<body oncontextmenu="return false">
...
</body>
Nikunj Aggarwal
  • 423
  • 2
  • 7
  • 18
2
window.oncontextmenu = function () {
return false;
}

might help you.

SK.
  • 4,174
  • 4
  • 30
  • 48
1

Try this : write below code on body & feel the magic :)

body oncontextmenu="return false"
John
  • 697
  • 4
  • 13
  • 27
0

//Disable right click script via java script code

<script language=JavaScript>
//Disable right click script
var message = "";
///////////////////////////////////
function clickIE() {
    if (document.all) {
        (message);
        return false;
    }
}

function clickNS(e) {
    if (document.layers || (document.getElementById && !document.all)) {
        if (e.which == 2 || e.which == 3) {
            (message);
            return false;
        }
    }
}
if (document.layers) {
    document.captureEvents(Event.MOUSEDOWN);
    document.onmousedown = clickNS;
} else {
    document.onmouseup = clickNS;
    document.oncontextmenu = clickIE;
}

document.oncontextmenu = new Function("return false")
</script>

Click here to see semo

thecodedeveloper.com
  • 3,220
  • 5
  • 36
  • 67