CKTIS / HTML
oncontextmenu — context menu event handler
The oncontextmenu attribute specifies JavaScript that handles the contextmenu event. It is available on all HTML elements; returning false cancels the event if it is cancelable.
Version: WHATWG HTML Living Standard; index snapshot date recorded separately
Syntax
oncontextmenu="JavaScript statements"oncontextmenu is a global HTML event handler attribute available on every HTML element. It corresponds to the contextmenu event associated with a context menu request; the attribute itself does not define a menu.
Its value is a JavaScript function body, not a URL or a function declaration. The event object is available as event. return false cancels the event if it is cancelable; it does not stop propagation.
Distinguish the HTML attribute oncontextmenu, the JavaScript property element.oncontextmenu, and the event name contextmenu used with addEventListener(). The example dispatches an event programmatically to check handling and cancellation without manually opening a menu.
Examples
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>oncontextmenu example</title>
<button id="target" oncontextmenu="this.textContent = event.type; return false;">Context menu target</button>
<pre id="result"></pre>
<script>
const target = document.getElementById('target');
const testEvent = new Event('contextmenu', { cancelable: true });
target.dispatchEvent(testEvent);
document.getElementById('result').textContent =
target.textContent + '\ndefaultPrevented: ' + testEvent.defaultPrevented;
</script>
</html>Result
contextmenu defaultPrevented: true
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.