最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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.]

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.]

Share Improve this question edited Jan 16, 2022 at 18:33 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Sep 19, 2011 at 20:55 M. Anthony AielloM. Anthony Aiello 1,1041 gold badge11 silver badges21 bronze badges 3
  • FF has an option to disable right-click disablers, buried in the advanced javascript options ("disable or replace context menus"). – Marc B Commented Sep 19, 2011 at 20:59
  • This option is set (enabled) on both my Mac version of Firefox and my Windows version of Firefox, so there must be something else going on here. – M. Anthony Aiello Commented Sep 19, 2011 at 21:10
  • Did you do this for click too? Or just mousedown and mouseup? – Boris Zbarsky Commented Sep 20, 2011 at 2:38
Add a comment  | 

3 Answers 3

Reset to default 18

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)

  1. 暂无评论