The contextmenu event fires when the user tries to open a context menu—usually with a right-click or the context menu key. Learn preventDefault for custom menus, clientX / clientY, addEventListener vs oncontextmenu, Firefox Shift quirks, and five try-it labs.
01
Kind
Instance event
02
Type
PointerEvent
03
When
Right-click / menu key
04
Handler
oncontextmenu
05
Cancel
preventDefault()
06
Status
Limited availability
Fundamentals
Introduction
Browsers show a built-in context menu when you right-click (copy, inspect, open link, and so on). The contextmenu event is your chance to react—log the action, block the default menu, or show a custom menu for your app.
Any right-click that was not disabled earlier (for example by canceling the related click path) typically leads to a contextmenu event on the target.
💡
Beginner tip
Call event.preventDefault() to hide the browser menu, then place your own menu near event.clientX and event.clientY. Always offer another way to open the same actions on touch devices.
Concept
Understanding contextmenu
An instance event that answers: “Is the user trying to open a context menu on this element?”
Triggers — right mouse button, context menu key (and similar).
PointerEvent (inherits MouseEvent) in modern specs.
Cancelable — preventDefault() can stop the browser menu.
Useful fields — clientX, clientY, target.
Limited availability on MDN (not Baseline)—test mobile carefully.
Foundation
📝 Syntax
Use the event name with addEventListener, or set the handler property:
A PointerEvent (inherits from MouseEvent and Event).
Handy inherited properties
Property
Meaning
clientX / clientY
Pointer position in the viewport (great for placing a custom menu)
button
Mouse button (right-click is typically 2)
target
Element that received the event
Quirk
⚠️ Firefox Shift + Right-Click
MDN notes a Firefox exception: if the user holds Shift while right-clicking, Firefox may show the context menu without firing contextmenu. In that case preventDefault() cannot hide the menu because your handler never runs.
Treat custom menus as progressive enhancement, not a security lock. Users can still use other browser chrome or OS features.
Compare
⚖️ contextmenu vs click / auxclick
Event
Typical trigger
click
Primary button activation
auxclick
Non-primary buttons (often middle / sometimes right)
contextmenu
Attempt to open a context menu (right-click / menu key)
Cheat Sheet
⚡ Quick Reference
Goal
Code / note
Listen
el.addEventListener("contextmenu", fn)
Handler property
el.oncontextmenu = fn
Disable browser menu
event.preventDefault()
Place custom menu
event.clientX, event.clientY
MDN status
Limited availability (not Baseline)
Snapshot
🔍 At a Glance
Four facts to remember about contextmenu.
Event type
PointerEvent
MouseEvent fields too
Means
open menu?
Right-click / key
Custom UI
preventDefault
Then show your menu
Baseline
no
Limited availability
Hands-On
Examples Gallery
Examples follow MDN Element: contextmenu event. Right-click the demo targets (or use the context menu key where available).
📚 Getting Started
Cancel the browser menu and listen with the handler property.
Example 1 — Cancel with preventDefault() (MDN)
MDN idea: disable the context menu on one paragraph only.
Custom menu appears at the pointer; click elsewhere to hide
How It Works
This is a teaching sketch. Production menus need keyboard focus, Escape to close, and accessibility roles.
Example 5 — MDN Two-Paragraph Demo
Full MDN HTML+JS pattern: one paragraph blocks the menu; the other does not.
JavaScript
<p id="noContextMenu">The context menu has been disabled on this paragraph.</p>
<p>But it has not been disabled on this one.</p>
<script>
const noContext = document.getElementById("noContextMenu");
noContext.addEventListener("contextmenu", (e) => {
e.preventDefault();
});
</script>
contextmenu is marked Limited availability on MDN (not Baseline). Logos use the shared browser-image-sprite.png sprite from this project. Desktop browsers generally support it; iOS Safari typically does not fire the event—ship an alternate menu control for touch.
✓ Limited availability
Element contextmenu
Strong on desktop right-click / menu-key paths; incomplete on some mobile browsers such as iOS Safari.
LimitedNot Baseline
Google ChromeSupported · Desktop & Android (verify touch UX)
Supported
Mozilla FirefoxSupported · Shift+right-click quirk
Supported
Apple SafariDesktop supported; iOS typically no event
Partial
Microsoft EdgeSupported · Chromium
Supported
OperaSupported · Modern versions
Supported
Internet ExplorerLegacy support for basic contextmenu
Legacy
contextmenuLimited
Bottom line: Use contextmenu for custom desktop menus with preventDefault; always provide a visible menu button for mobile users.
Wrap Up
Conclusion
contextmenu is the “user wants a context menu” signal. Listen with addEventListener, optionally call preventDefault(), and place a custom menu with clientX / clientY—while remembering Limited availability on some mobile browsers.
Call preventDefault() only when you show a real custom menu
Position menus with clientX / clientY
Provide a visible alternate control for touch / iOS
Close the custom menu on outside click or Escape
❌ Don’t
Disable context menus site-wide without a good reason
Assume iOS Safari will fire contextmenu
Treat menu blocking as content protection
Forget Firefox Shift+right-click can bypass your handler
Overwrite oncontextmenu if you need multiple listeners
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about contextmenu
Right-click signal—cancel default, show custom UI, support mobile another way.
5
Core concepts
🖱️01
Right-click
or menu key
Trigger
📄02
PointerEvent
clientX / clientY
API
🚫03
preventDefault
hide browser menu
Pattern
📱04
Mobile gap
offer a button
UX
🎯05
Limited
not Baseline
Compat
❓ Frequently Asked Questions
It fires when the user attempts to open a context menu — typically by right-clicking or pressing the context menu key on the keyboard.
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.
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.
Did you know?
When the context menu key (not the mouse) opens a menu, browsers often place it near the bottom-left of the focused element—or the current row in a tree control—rather than at a pointer coordinate.