This was the closest I could come. I didn't like it that much though because I had to rely on a global. I tried other things like canceling the onmousedown event, but nothing I did could subsequently cause the onclick event from firing.
Incidentally I noticed that rightclick throws an onclick event in FF, but not in Chrome. I didn't check IE.
//global to store whether we rightclicked
var rightclick = false;
//use mousedown to detect rightclick
document.onmousedown = function (e) {
e = e || window.event;
switch (e.which) {
case 1:
console.log('left');
break;
case 2:
console.log('middle');
break;
case 3:
console.log('right');
rightclick = true; //store click in global
break;
}
};
//in onclick, which fires after onmousedown, check whether it was from a right click
document.onclick = function (e) {
if (rightclick) {
rightclick = false;
return;
}
console.log('onclick');
};
document.addEventListener("contextmenu", function (e) {
e.preventDefault();
}, false);