Using onContextMenu: How to prevent Firefox's context menu

Hopefully the topic title says it all. I have added a context menu to my project. In IE, the default browser context menu is successfully suppressed but Firefox’s still appears, on top of the project menu. This is sub-optimal!

How can I get FF’s menu to not appear?

Hi,
returning false from onContextMenu event should do, i.e. in this demo default menu should be prevented when you right click on event boxes:
docs.dhtmlx.com/scheduler/sampl … xMenu.html

  • does it work the same way for you?
    If you want to prevent native menu always you need to make sure onContextMenu always returns false - in our demo it allows native menu if you click anywhere other than event box.
    You can also try event.preventDefault() and/or event.stopPropagation() if return false doesn’t work for some reason

As I mentioned, in IE, the context menu is successfully prevented from appearing. It’s when the page is loaded in Firefox that its menu overlays mine. Your demo page behaves the same way.

Pressing ‘Esc’ dismisses FF’s menu leaving mine displayed but obviously this is not ideal.

Have you tried event.preventDefault/event.stopPropagation ?
I.e.[code]scheduler.attachEvent(“onContextMenu”, function(event_id, native_event_object) {
if (event_id) {
//… do something

	native_event_object.preventDefault();//!!
	native_event_object.stopPropagation();//!!

	return false; // Prevent default action and propagation
}
return true;

});[/code]
developer.mozilla.org/en/docs/W … entDefault
developer.mozilla.org/en-US/doc … ropagation

I just tried that, to no avail. Remember: this is only in FF. IE behaves itself.

Not sure what may be wrong.
Usually returning false from html ‘contextmenu’ event handler should be enough to prevent menu from appearing,
this is exactly what happens when you return false from ‘onContextMenu’ of scheduler: github.com/DHTMLX/scheduler/blo … r.js#L2608

Seems like FF have a setting that could prevent code from disabling the native context menu support.mozilla.org/t5/Firefox/ … -p/1351434 , although I’m not sure that’s the case. If I check locally - default context menu won’t show up in FF if canceled from onContextMenu handler.

As a quick check, you can create empty html page, make html,body 100% width/height and add code that prevents context menu, e.g.

document.addEventListener('contextmenu', function(e) { e.preventDefault(); }, false);
If it works the same way (prevents context menu in IE/Chrome but fails in FF) - then it’s something with your FF settings. Probably there could be a workaround, like preventing mousedown event for rightmouse button.
If it works correctly in all browsers - then the issue is in your handler code

about:config tells me that the ‘dom.event.contextmenu.enabled’ setting is ‘User set’ and is ‘false’ so, by any reckoning, that means that the default context menu should be disabled. Deep joy.

I think my handler code has to be OK because it’s only FF that misbehaves.

Where to now? :frowning:

according to this, dom.event.contextmenu.enabled=false should do exactly what you experiencing
kb.mozillazine.org/About:config_entries

  • is that’s the case, then I think there is no issue here - you’ve explicitly configured firefox not to allow replacing default context menus, which it successfully does.
    I.e. if you want to have a custom context menu, you shouldn’t tell browser to prevent them:) The solution would be to change that setting back to default value.

Alternatively, you could try different workarounds, such as preventing mousedown for a right button, which should eventually prevent context menu as well:document.addEventListener('mousedown', function(e) { if (e.button == 2) { e.preventDefault(); } }, false);

  • although i’m not 100% sure it would work.

My suggestion is that the issue should be fixed by allowing custom menus in FF settings, rather than by finding a hack to bypass that setting.