The oncontextmenu attribute is an event-handler attribute. It runs JavaScript when the contextmenu event fires — usually a right-click, or the keyboard context menu key, when the user tries to open a context menu.
Use it to log the action, call preventDefault() to suppress the browser menu, or open a custom menu. Always offer the same actions through normal UI (buttons, menus) so users who cannot right-click are not blocked.
Limited availability — not Baseline.
MDN marks contextmenu as Limited availability: it does not work consistently in some widely used browsers. Test your targets; treat custom menus as enhancement.
Prefer addEventListener in apps.
Inline oncontextmenu is fine for learning. Production code usually uses el.addEventListener("contextmenu", handler). Strict CSP may also block inline handlers.
Syntax
HTML Syntax
syntax.html
<element oncontextmenu="script">
script is JavaScript that runs when contextmenu fires. Equivalent JS: el.oncontextmenu = handler or el.addEventListener("contextmenu", handler).
Values
Attribute Values
The attribute value is always a script. Common patterns:
Value / pattern
Description
Try it
script
Any JavaScript run when a context menu is requested
Long-press menus differ by OS and browser. Never make core features right-click-only on the web.
Compatibility
Browser Support
oncontextmenu is an HTML event-handler attribute in the browser. It is not a Node.js, Deno, or Bun API.
✓ Limited availability
oncontextmenu
Works in many desktop browsers for right-click and the menu key, but MDN lists it as Limited availability (not Baseline). Mobile and some engines are incomplete — test before depending on it.
~85%Global support (approx.)
Google ChromeModern
Supported
Mozilla FirefoxModern
Supported*
Apple SafariCheck version
Partial / check
Microsoft EdgeModern
Supported
OperaModern
Supported
Samsung InternetVaries
Check
Internet ExplorerLegacy
Supported
oncontextmenuLimited / check targets
Bottom line: Use for enhancement, not the only path to an action. *Firefox: Shift+right-click may bypass your handler.
Remember
Key Takeaways
Role: run script when the user requests a context menu.
Cancel:preventDefault() blocks the browser menu (with caveats).
Support: Limited availability — not Baseline; test platforms.
A11y: never hide critical actions behind right-click only.
One line:oncontextmenu reacts to a menu request — enhance carefully; always keep a non–right-click path.
Frequently Asked Questions
It is an event-handler attribute. When the user tries to open a context menu — usually with a right-click or the context menu key — the contextmenu event fires and the JavaScript in oncontextmenu runs.
Call event.preventDefault() inside the handler. That cancels the default menu so you can show your own UI instead. Some browsers still allow a bypass (for example Shift+right-click in Firefox).
No. MDN marks the contextmenu event as Limited availability — it is not Baseline because support is incomplete in some widely used browsers. Test your target platforms.
Support and gestures vary. Long-press may open a context menu on some devices, but behavior is not as consistent as desktop right-click. Always provide another way to reach the same actions.
Both work where the event is supported. For learning, oncontextmenu in HTML is fine. In production code, prefer element.addEventListener("contextmenu", handler).
It is not deprecated. It is also not experimental in the sense of a brand-new API, but it is Limited availability (not Baseline). Treat custom menus as progressive enhancement.