The oncontextmenu
attribute event is triggered
when opening the context menu.
We can normally open the context menu by right-clicking on an element.
The oncontextmenu
attribute is new in HTML5.
<element oncontextmenu="script or Javascript function name">
All HTML elements.
oncontextmenu |
Yes | Yes | Yes | Yes | Yes |
<!DOCTYPE html>
<html>
<head>
<style>
div {<!--from ww w . java 2 s.co m-->
border: 1px solid black;
padding: 10px;
}
</style>
</head>
<body>
<div oncontextmenu="myFunction()" contextmenu="mymenu">
Right-click inside this box to see the context menu!
</div>
<script>
function myFunction() {
alert("right-clicked inside the div!");
}
</script>
</body>
</html>