views:

91

answers:

1

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

+3  A: 

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.

Alsciende
Nice! Think it can be adapted to jquery though? I'm not using prototype
Jonah
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
Thanks a ton man
Jonah
Both links are giving me an error in IE8
Josh Stodola
Oops that's right, error in IE7 too. DOM FTW. I'll edit my answer.
Alsciende