HTML oncontextmenu Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

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.

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

Attribute Values

The attribute value is always a script. Common patterns:

Value / patternDescriptionTry it
scriptAny JavaScript run when a context menu is requestedTry it
oncontextmenu="fn()"Call a named function from the attributeTry it
preventDefault()Cancel the browser’s default context menuTry it
addEventListener("contextmenu")Preferred modern listener; easy to combine with preventDefaultTry it
Custom menu UIShow your own menu at the pointer (keep a non–right-click path too)Try it

Live Preview

Right-click (or use the context menu key) inside the box. Toggle preventDefault to block the browser menu.

Options

On some browsers (e.g. Firefox), Shift+right-click may still show the system menu.

Target

Right-click here (or focus and use the menu key).

Waiting for contextmenu…

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Inline oncontextmenu

Update status text when the user requests a context menu.

HTML
<p id="box" oncontextmenu="handleMenu(event)">
  Right-click this paragraph.
</p>
<p id="status">Waiting…</p>

<script>
  function handleMenu(event) {
    document.getElementById("status").textContent =
      "contextmenu at " + event.clientX + ", " + event.clientY;
  }
</script>
Try It Yourself

How It Works

  1. User opens a context menu on the paragraph (right-click or menu key).
  2. contextmenu fires and oncontextmenu runs handleMenu(event).
  3. Read pointer position from event.clientX / clientY.
  4. Default menu still opens unless you call preventDefault().

Example 2 — Block the Default Menu

Cancel the browser menu with preventDefault().

HTML
<p id="noMenu">Right-click: default menu blocked.</p>
<p>Right-click: normal browser menu.</p>

<script>
  document.getElementById("noMenu").addEventListener("contextmenu", function (e) {
    e.preventDefault();
  });
</script>
Try It Yourself

How It Works

  1. Listen for contextmenu on the first paragraph only.
  2. Call e.preventDefault() to cancel the default menu.
  3. Second paragraph has no listener — the browser menu still appears.
  4. Bypass note: in Firefox, Shift+right-click may show the menu without firing the event.

Example 3 — Simple Custom Menu

Hide the default menu and show a small custom panel at the pointer.

HTML
<div id="area" tabindex="0">Right-click for a custom menu.</div>
<div id="menu" hidden>
  <button type="button" id="copyBtn">Copy label</button>
</div>
<p id="log"></p>

<script>
  var area = document.getElementById("area");
  var menu = document.getElementById("menu");
  var log = document.getElementById("log");

  area.addEventListener("contextmenu", function (e) {
    e.preventDefault();
    menu.hidden = false;
    menu.style.position = "fixed";
    menu.style.left = e.clientX + "px";
    menu.style.top = e.clientY + "px";
  });

  document.getElementById("copyBtn").addEventListener("click", function () {
    log.textContent = "Chose: Copy label";
    menu.hidden = true;
  });
</script>
Try It Yourself

How It Works

  1. Prevent the default menu so your UI can take over.
  2. Position a simple panel near clientX / clientY.
  3. Use real <button>s inside the menu for keyboard and screen-reader support.
  4. Also expose actions elsewhere (toolbar, visible buttons) — not only via right-click.

Edge Cases & Pitfalls

Most issues are support gaps, browser bypasses, or hiding actions behind right-click only.

support

Not Baseline

Limited availability means some browsers or platforms are incomplete. Feature-detect and keep fallback UI.

firefox

Shift+right-click may bypass

In Firefox, holding Shift while right-clicking can show the system menu without firing contextmenu — so preventDefault never runs.

a11y

Do not rely on right-click alone

Many users cannot or do not right-click. Duplicate important actions with visible buttons or menus.

vs click

Not the same as onclick

Primary button / activation → click. Context menu request → contextmenu.

mobile

Touch behavior varies

Long-press menus differ by OS and browser. Never make core features right-click-only on the web.

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 Chrome Modern
Supported
Mozilla Firefox Modern
Supported*
Apple Safari Check version
Partial / check
Microsoft Edge Modern
Supported
Opera Modern
Supported
Samsung Internet Varies
Check
Internet Explorer Legacy
Supported
oncontextmenu Limited / check targets

Bottom line: Use for enhancement, not the only path to an action. *Firefox: Shift+right-click may bypass your handler.

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.

Next: oncopy

Run JavaScript when the user copies selected content.

oncopy tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful