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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Jonah
  • 2,040
  • 7
  • 29
  • 32

1 Answers1

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.

Alsciende
  • 26,583
  • 9
  • 51
  • 67
  • Nice! Think it can be adapted to jquery though? I'm not using prototype – Jonah Jun 04 '10 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 07 '10 at 07:55
  • Oops that's right, error in IE7 too. DOM FTW. I'll edit my answer. – Alsciende Jun 11 '10 at 14:19