6

I'm using a context-menu jQuery plugin and I need to detect what browsers support this. How can this be done?

I heard some versions of Opera and Safari don't support this right-click overriding business.

1

1 Answer 1

4

You can create and fire a contextmenu event manually. If you set the proper handler, you can detect if the handler is called or not.

Here is an example (using jQuery for event creation and observation):

function testContextMenuEvent() {
  var supported = false;
  function handler(e) {
    supported = true;
    e.stopPropagation();
  }
  $(document).bind('contextmenu', handler);
  var evt = jQuery.Event("contextmenu");
  $(document).trigger(evt);
  $(document).unbind('contextmenu', handler);
  return supported;
}

Here is a test page : http://jsfiddle.net/Hk4xA/6/

edit2: the DOM has striken again. I forgot that createEvent totally doesn't work on IE. So instead I used jQuery for the event creation too.

3
  • Nice! Think it can be adapted to jquery though? I'm not using prototype
    – Jonah
    Jun 4, 2010 at 15:45
  • Of course. Prototype is used lines 5, 7 and 11. jQuery's event API is just different names for similar methods here: observe -> bind, stopObserving -> unbind, stop -> preventDefault. See my edit for an example.
    – Alsciende
    Jun 7, 2010 at 7:55
  • Oops that's right, error in IE7 too. DOM FTW. I'll edit my answer.
    – Alsciende
    Jun 11, 2010 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.