html tutorial - oncontextmenu Attribute in HTML - html5 - html code - html form

Learn html - html tutorial - Oncontextmenu attribute in html - html examples - html programs
- The oncontextmenu attribute specifies through the user right-clicks on an element to open the context menu.
- The oncontextmenu attribute is section of the Event Attributes.
Syntax for oncontextmenu attribute in HTML:
<element oncontextmenu = “script”>
Differences between HTML 4.01 and HTML5 for oncontextmenu attribute:
HTML 4.01
- HTML4 does not support oncontextmenu attribute.
HTML 5
- HTML5 support oncontextmenu attribute.
Applies To:
Element | Attribute |
---|---|
All HTML Elements | oncontextmenu |
Attribute Values:
Value | Description |
---|---|
script | The script can run on oncontextmenu. |
Sample Coding for oncontextmenu Attribute in HTML:
Tryit<!DOCTYPE html>
<html>
<head>
<title>Wikitechy oncontextmenu attribute</title>
</head>
<body>
<div style="background:cyan; padding:20px;" oncontextmenu="onContextMenu()" contextmenu="wikiContext">
<p>Right click the box to View Wikitechy Context Menu </p>
<menu type="context" id="wikiContext">
<menuitem label="Wikitechy" onclick="window.open('http://www.wikitechy.com');" icon="icon.png">
</menuitem>
<menu label="Online">
<menu type="context" id="wikiContext">
<menuitem label="Tutorials" icon="icon.png" onclick="window.open
('http://www.wikitechy.com/online-tutorials');">
</menuitem>
</menu>
</menu>
</menu>
</div>
<script>
function onContextMenu()
{
alert("You right-clicked inside the div!");
}
</script>
</body>
</html>
Code Explanation for oncontextmenu Attribute in HTML:

- oncontextmenu attribute is used to call the onContextMenu() function when the user Right-click the <div> element content.
- JavaScript function onContextMenu() is used to display an alert message.
Output for oncontextmenu Attribute in HTML:

- The div content was displayed, when the page has been loaded.

- When user right click over div element, the alert box has been displayed.
- After clicking the OK button in alert box, the context menu has been displayed.
Browser Support for oncontextmenu Attribute in HTML:
![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|
No | No | Yes | No | No |