Latest web development tutorials
 

oncontextmenu Event

< Event Object

Example

Execute a JavaScript when the user right-clicks on a <div> element with a context menu:

<div oncontextmenu="myFunction()" contextmenu="mymenu">
Try it Yourself »

Definition and Usage

The oncontextmenu event occurs when the user right-clicks on an element to open the context menu.

Note: Although the oncontextmenu event is supported in all browsers, the contextmenu attribute is currently only supported in Firefox.


Browser Support

Event
oncontextmenu Yes Yes Yes Yes Yes

Syntax

In HTML:

<element oncontextmenu="myScript">
Try it Yourself »

In JavaScript:

object.oncontextmenu=function(){myScript};
Try it Yourself »

In JavaScript, using the addEventListener() method:

object.addEventListener("contextmenu", myScript);
Try it Yourself »

Note: The addEventListener() method is not supported in Internet Explorer 8 and earlier versions.


Technical Details

Bubbles: Yes
Cancelable: Yes
Event type: MouseEvent
Supported HTML tags: All HTML elements
DOM Version: Level 3 Events

< Event Object