Question:
HTML (how to do no right click with no message)?
anonymous
2012-01-21 19:35:38 UTC
trying to use the following piece of code to prevent right clicks







This should work for all modern browsers.



There are two separate event paths that occur when the user right clicks. One is the typical mousedown => mouseup => mouseclick path (the events fire in that order... mostly :) ). Your original code intercepted this path to detect when the right mouse button was clicked. All fine and dandy, but it doesn't prevent the context menu that appears by default when a right-click occurs.



This is the second event path. The browser also interprets a right mouse click as a request for the context menu. To prevent the context menu from appearing, you need to intercept this event by using oncontextmenu, and cancel the event.



Every event handler is automagically passed an Event instance as an argument: in my code, I called it "e". To cancel an event, it's wise to both set e.returnValue to false, and return false from the function itself. It's a little redundant, but doesn't hurt :)
?
2012-01-22 03:39:37 UTC
?
2012-01-22 03:40:15 UTC
remove alert() from your code


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...