tags:

views:

173

answers:

7

Is right click an javascript event? If so, how do I use it?

A: 

Yes - it is!

function doSomething(e) {
    var rightclick;
    if (!e) var e = window.event;
    if (e.which) rightclick = (e.which == 3);
    else if (e.button) rightclick = (e.button == 2);
    alert('Rightclick: ' + rightclick); // true or false
}
NickAtuShip
Well - technically its not an "event" but it can be used.
NickAtuShip
Your code doesn't show at all how someone would call this...
Timothy Khouri
A: 

No, but you can detect what mouse button was used in the "onmousedown" event... and from there determine if it was a "right-click".

Timothy Khouri
so should a right click trigger an onclick event?
Brian
You're right, that would be onmousedown.
Timothy Khouri
@Brian: a right click will trigger `onmousedown`, `onmouseup` and `onclick`. If you're looking at overriding the right-click menu, however, these events are not what you should be using (see my answer below).
Andy E
A: 

Kinda, check this out: http://www.rgagnon.com/jsdetails/js-0061.html

Dustin Laine
A: 

Yes, its a javascript mousedown event. There is a jQuery plugin too to do it

Teja Kantamneni
A: 

Yes, it's called MouseEvent. See http://www.w3.org/TR/2000/REC-DOM-Level-2-Events-20001113/events.html#Events-MouseEvent for the spec.

For an example, see e.g. http://unixpapa.com/js/mouse.html .

sleske
+5  A: 

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

Andy E
Just like with the which/button issue, the 'oncontextmenu' is not implemented the same in all browsers... see http://www.quirksmode.org/dom/events/contextmenu.html for some of the 'gotchas'.
smencer
@smencer: right you are. In this case a combination of `onclick` and `onkeyup` would be required (for shift+F10 or context menu key).
Andy E
A: 

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 1 for the left button, 2 for the middle button, or 3 for the right button.

henchman