开发者

preventDefault blocks right-click menu in Firefox on Mac but not Windows

开发者 https://www.devze.com 2023-04-06 04:02 出处:网络
I have a web application in which I have hooked mouse up and mouse down events; I use them for selection and manipulation of the graphical language for which my application is an editor. To prevent th

I have a web application in which I have hooked mouse up and mouse down events; I use them for selection and manipulation of the graphical language for which my application is an editor. To prevent the right-click/context menu supplied by Firefox from showing up, I've placed:

if (evt.preventDefault) {
  evt.preventDefault();
}

at the top of each of my mouse up and mouse down event handlers. I don't want to return false; I actually want the event to propagate.

On the Mac, the right-click menu doesn't show up; this is what I expect. On Windows, however, it stubbornly appears, even though Firebug confirms that my call to "preventDefault" is occurring and likewise "defaultPrevented" gets set to true.

Any idea what gives? Has anyone else run across this problem? I'm running Firefox 6.0.2 on both the Mac and Windows.

开发者_如何学编程

[Update: more recent versions of Firefox yielded consistent results on Mac and Windows: the context menu failed to be suppressed on both platforms.]


Okay. After putting this aside and returning to it several times, I finally found the solution.

Attempting to deal with the appearance of the context menu in the various mouse listeners appears to be fundamentally flawed. Instead, thanks to code I found here, I was put on the scent of the contextmenu event. That event appears to be the right way to handle things, although the code actually posted on that site didn't do the trick — merely calling "stopPropagation" and returning false was insufficient.

The following worked for me:

element.addEventListener('contextmenu', function(evt) { 
  evt.preventDefault();
}, false);

This has been tested with Firefox 10.0 on a Mac and Firefox 9.0.1 and 10.0 on Windows 7.


This option is removed in Mozilla's 23rd version.


  1. Go to Tools > Options.
  2. Go to the Content tab.
  3. Click Advanced button next to Enable JavaScript option.
  4. Disable or replace context menus. Check this box and it will magically work again.

There is no way to get around this setting in JavaScript.

0

精彩评论

暂无评论...
验证码 换一张
取 消