I'm trying to work with the contextmenu & click handlers on an anchor element in a cross-browser way, and it's proving really difficult.
Here's the problem: if preventDefault() is called on a contextmenu event from a contextmenu event handler, browsers behave differently. All browsers correctly do not display their browser's default context menu. In Chrome & Safari, however, the browser then follows a click event for the element (and only if the contextmenu event is canceled).
When I cancel a contextmenu event from a contextmenu handler, I do NOT want a successive click event fired on the same element. Is there a clean way to resolve this? Neither returning false on the contextmenu handler nor the calling stopPropagation function alleviate the issue!
Here is a demo with a strange glitch (use Webkit inspector or Firebug to see the trace)
Here's one answer: use jQuery rather than POJ to cleanup width/height stuff:
function Cancel_Next_Click()
{
// Compatibility - Chrome & Safari bug where context click event is followed by an undesired click event
var Cancel_Next_Click_Element = document.createElement("div");
Cancel_Next_Click_Element.style.position = "fixed";
Cancel_Next_Click_Element.style.left = "0px";
Cancel_Next_Click_Element.style.top = "0px";
Cancel_Next_Click_Element.style.width = window.innerWidth;
Cancel_Next_Click_Element.style.height =window.innerHeight;
document.body.appendChild(Cancel_Next_Click_Element);
var Cancel_Next_Click_Element_Listener = function() {
document.body.removeChild(Cancel_Next_Click_Element);
document.removeEventListener('mouseup', Cancel_Next_Click_Element_Listener, false);
}
document.addEventListener('mouseup', Cancel_Next_Click_Element_Listener, false);
}
by using some other element like a span for instance you wouldn't have to worry about the default action, since there wouldn't be a click even by default. I found these two jquery plugins that might do the trick for you:
http://abeautifulsite.net/blog/2008/09/jquery-context-menu-plugin/
http://beckelman.net/post/2008/11/04/Right-or-Left-Click-Context-Menu-Using-jQuery-Demo.aspx
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