Use right-click event to add Onmousedown= "if" (Event.button = = 2) alert (' Right click ') where you need the right button! '); You can inadvertently be asked by a colleague in JavaScript how to detect right-click events, and block the original right-click menu, the Internet to find a turn after the discovery of some relatively simple method.
If you set onmousedown, check that the value of its Event.button is 2 (for right-click).
This method is available in both FF and IE, but in Maxthon Event.button is 0, which makes me a bit confused, maxthon not ie kernel?
I can only imagine that the shell of Maxthon has been tampered with. However, if OnMouseUp is set, its Event.button value is 2.
So if the right button is detected, it is set to onmouseup.
document.getElementById ("Test"). Onmouseup=function (oevent) {
if (!oevent) oevent=window.event;
if (oevent.button==2) {
--Do something for user right click
Alert ("Mouse up");
}
}
But if you still need to shield the right button, or use OnContextMenu relatively simple, but this is not the right to detect, but to detect whether the context menu popup.
The method of shielding is the same as blocking other default behavior, which is generally valid, but because some browsers have the ability to prohibit the popup right-click menu, so if you need to do something when the user right click, it is best not to put in the OnContextMenu, Instead, it is placed in the onmouseup and detects the right button, attaching oncontextmenu to block the original menu.
document.getElementById ("Test"). Oncontextmenu=function (Event) {
--Do something here
Alert ("ContextMenu Popup");
--Prevent the default behavior
if (document.all) Window.event.returnValue = false;//for IE
else Event.preventdefault ();
};
With some simple tests, there are interesting differences in the FF and in IE.
Using alert in OnMouseUp and OnContextMenu event processing, you can see that the OnMouseUp event is executed first and then OnContextMenu, and in IE, the two are executed together very coherently, (both are considered to occur on the test element), but in the FF is not (if the test element occupies a relatively small area, when the alert pops up need to move the mouse to click on the "OK" case), it will first execute onmouseup,alert out, move the mouse click ' OK ', the menu will still pop up, but if you do not move the mouse and press ENTER to confirm it, then it will be considered an event triggered on the test element. It can be understood that the details of the event mechanism in IE and Firefox are different. Of course, we rarely apply to continuous events, there is no need to notice this difference, the need to write the event to complete in a processing method is the simplest and most effective solution.
JavaScript handles right mouse button events