Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 945
M. Anthony Aiello Avatar asked Sep 19 '11 20:09

M. Anthony Aiello


People also ask

How do I disable right click menu in Firefox?

On the menu bar in the Firefox browser window, click Tools > Options. On the Options window, select Content. Click Advanced by the Enable JavaScript setting. Select Disable or replace context menus.

How do I hide the context menu in right click?

The context menu usually has a list of actions like "View Page Source" and "Back". We can prevent this menu from appearing on right-click by latching onto the contextmenu event and using event. preventDefault() . If we add this event listener to the window object then we can prevent right-clicking on the whole page.

Can I disable context menu?

Navigate to User Configuration > Administrative Templates > Start Menu and Taskbar. Double-click on the Disable context menus in the Start Menu option.


1 Answers

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.

like image 104
M. Anthony Aiello Avatar answered Nov 15 '22 10:11

M. Anthony Aiello