views:

158

answers:

4

Possible Duplicate:
How do I disable right click on my web page .

How can I disable right click in web-pages that I am developing?

+10  A: 

Don't.

Right-click is used for contextual menus in many browsers (or Command-Click on Mac) and is very useful. Disabling it is a quick way to get some of your users angry.

If you are trying to prevent copying of elements... see one of the many threads already on the subject.

http://stackoverflow.com/questions/737022/how-do-i-disable-right-click-on-my-web-page

JYelton
i think he wants to disable image saving and copy pasting
Junaid Saeed
agreed, I hate disabled right click as i use it frequently myself.
Chris
good point; this is a duplicate of that question.
DDaviesBrackett
I can get them from the cache, disable javascript, screen capture... any number of ways. My right-click menu is used for navigation, Web Developer tweaking, and a host of other add-on functions. Monkeying with that will guarantee I turn off (or edit) scripts and start looking for security holes!
JYelton
A: 

You can catch right-click by attaching an event handler to the 'click' event of the document. If you return false from that event, the default action (popping up a menu) will be suppressed.

That said, if you're trying to make it so that people can't look at your source code, you're not going to be able to. The availability of developer tools like Firebug, IE's Developer Tools, and Webkit (aka Chrome)'s Inspect Element, plus people's local HTTP proxies and so on, mean that there will always be a way for interested people to see your source.

DDaviesBrackett
+3  A: 

As everyone suggested, you probably shouldn't.

But just in case yours is one of those rare cases in wich doing this is a legitimate requirement, there seems to be several ways to do it:

Editing the body tag:

<body oncontextmenu="return false;">

As shown in:

http://javascript.about.com/library/blnoright.htm

Or several scripts that do similar things in other ways:

http://www.hypergurl.com/norightclick.html

Sebastián Grignoli
A: 

Hunt down your users one by one, wrest the mouse from their grip, and physically tear off the button, preventing the from ever right clicking anything again!

Or, as an alternative, not, but you might as well, considering how you are treating them.


Why do think you need to disable right clicking? What, exactly are you trying to achieve? If your goal is somehow to prevent content theft, I've got bad news for you. There is in practice no way of reliably enforcing DRM, much less when said protection is implemented in JavaScript, as this would leave any protection laughably easy to circumvent for anyone who wants what you are trying to protect, and will only serve to frustrate and inconvenience legitimate users.

If there is any other reason to want to do this, I'm not seeing it.

Williham Totland