In JavaScript, the contextmenu event fires when the user attempts to open a context menu — typically by right-clicking or pressing the context menu key (MDN).
Register a listener with addEventListener. Check readiness and browser support before you rely on this event.
Syntax
Listen for contextmenu. Start with this line.
JavaScript
addEventListener("contextmenu", fn)
Call
What it does
addEventListener("contextmenu", fn)
Primary signature.
Built into the browser. Listen with el.addEventListener("contextmenu", …) — no import required.
Library declarations
JavaScript
addEventListener("contextmenu", fn)
Parameters
Parameter
Description
Event name
Required. "contextmenu"
Handler prop
Optional. oncontextmenu
Return Value
Return Value
Description
*
Fires as an Element event (listener receives the Event object).
JavaScript Examples
Example 1 — Cancel the Browser Menu (MDN)
Call preventDefault() so right-click does nothing on that element.
Cancel first.preventDefault() hides the browser menu.
Build your UI. Here we only write text — keep styling in CSS files in real apps.
Include coordinates. So you know where the user clicked.
Add a button fallback for platforms where contextmenu is unreliable.
Compatibility
Browser Support
Elementcontextmenu has Limited availability on MDN (not Baseline). Desktop browsers generally support it; some mobile browsers (notably iOS Safari) are incomplete. Always provide a non–right-click menu path.
✓ Limited availability
contextmenu
Fires when the user tries to open a context menu (right-click or menu key).
LimitedNot Baseline
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
Android WebViewSupported · Modern WebView
Full support
contextmenuLimited
Bottom line: Fine for desktop custom menus with preventDefault. Add a visible menu control for touch / Limited-availability platforms. Remember Firefox Shift+right-click.
Frequently Asked Questions
It fires when the user attempts to open a context menu — typically by right-clicking or pressing the context menu key (MDN).
No. MDN does not mark it Deprecated, Experimental, or Non-standard. It has Limited availability (not Baseline), mainly because support is incomplete on some platforms such as iOS Safari.
A PointerEvent (inherits from MouseEvent). Older specs used MouseEvent; check compatibility if you rely on PointerEvent-only fields.
Call event.preventDefault() in your contextmenu handler, then show your own UI — often positioned with event.clientX and event.clientY.
In Firefox, holding Shift while right-clicking shows the context menu without firing the contextmenu event, so canceling the event cannot block that path (MDN).
Desktop browsers generally fire contextmenu on right-click. On many touch devices (notably iOS Safari) the event may not fire; provide an alternate control such as a menu button for mobile users.