The contextmenu event fires when the user attempts to open a context menu. This event is typically triggered by clicking the right mouse button, or by pressing the context menu key.
JavaScript's interaction with HTML is handled through events that occur when the user or the browser manipulates a page. When the page loads, it is called an event. When the user clicks a button, that click too is an event. Other examples include events like pressing any key, closing a window, resizing a window, etc.
The MouseEvent interface represents events that occur due to the user interacting with a pointing device (such as a mouse). Common events using this interface include click , dblclick , mouseup , mousedown . MouseEvent derives from UIEvent , which in turn derives from Event .
As others have mentioned, the right mouse button can be detected through the usual mouse events (mousedown, mouseup, click). However, if you're looking for a firing event when the right-click menu is brought up, you're looking in the wrong place. The right-click/context menu is also accessible via the keyboard (shift+F10 or context menu key on Windows and some Linux). In this situation, the event that you're looking for is oncontextmenu
:
window.oncontextmenu = function ()
{
showCustomMenu();
return false; // cancel default menu
}
As for the mouse events themselves, browsers set a property to the event object that is accessible from the event handling function:
document.body.onclick = function (e) {
var isRightMB;
e = e || window.event;
if ("which" in e) // Gecko (Firefox), WebKit (Safari/Chrome) & Opera
isRightMB = e.which == 3;
else if ("button" in e) // IE, Opera
isRightMB = e.button == 2;
alert("Right mouse button " + (isRightMB ? "" : " was not") + "clicked!");
}
window.oncontextmenu - MDC
have a look at the following jQuery code:
$("#myId").mousedown(function(ev){
if(ev.which == 3)
{
alert("Right mouse button clicked on element with id myId");
}
});
The value of which
will be:
You could use the event window.oncontextmenu
, for example:
window.oncontextmenu = function () {
alert('Right Click')
}
<h1>Please Right Click here!</h1>
If you want to detect right mouse click, you shouldn't use MouseEvent.which
property as it is non-standard and there's large incompatibility among browsers. (see MDN) You should instead use MouseEvent.button
. It returns a number representing a given button:
0
: Main button pressed, usually the left button or the un-initialized state1
: Auxiliary button pressed, usually the wheel button or the middle button (if present)2
: Secondary button pressed, usually the right button3
: Fourth button, typically the Browser Back button4
: Fifth button, typically the Browser Forward buttonMouseEvent.button
handles more input types than just standard mouse:
Buttons may be configured differently to the standard "left to right" layout. A mouse configured for left-handed use may have the button actions reversed. Some pointing devices only have one button and use keyboard or other input mechanisms to indicate main, secondary, auxilary, etc. Others may have many buttons mapped to different functions and button values.
Reference:
Ya, though w3c says the right click can be detected by the click event, onClick is not triggered through right click in usual browsers.
In fact, right click only trigger onMouseDown onMouseUp and onContextMenu.
Thus, you can regard "onContextMenu" as the right click event. It's an HTML5.0 standard.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With