Is right click a Javascript event? If so, how do I use it?
If You want to call the function while right click event means we can use following
<html lang="en" oncontextmenu="func(); return false;">
</html>
<script>
function func(){
alert("Yes");
}
</script>
To handle right click from the mouse, you can use the 'oncontextmenu' event. Below is an example:
document.body.oncontextmenu=function(event) {
alert(" Right click! ");
};
the above code alerts some text when right click is pressed. If you do not want the default menu of the browser to appear, you can add return false; At the end of the content of the function. Thanks.
Ya, though w3c says the right click can be detected by the click event, onClick is not triggered through right click in usual browsers.
In fact, right click only trigger onMouseDown onMouseUp and onContextMenu.
Thus, you can regard "onContextMenu" as the right click event. It's an HTML5.0 standard.
Try using the which
and/or button
property
The demo
function onClick(e) {
if (e.which === 1 || e.button === 0) {
console.log('Left mouse button at ' + e.clientX + 'x' + e.clientY);
}
if (e.which === 2 || e.button === 1) {
console.log('Middle mouse button ' + e.clientX + 'x' + e.clientY);
}
if (e.which === 3 || e.button === 2) {
console.log('Right mouse button ' + e.clientX + 'x' + e.clientY);
}
if (e.which === 4 || e.button === 3) {
console.log('Backward mouse button ' + e.clientX + 'x' + e.clientY);
}
if (e.which === 5 || e.button === 4) {
console.log('Forward mouse button ' + e.clientX + 'x' + e.clientY);
}
}
window.addEventListener("mousedown", onClick);
Some info here
window.oncontextmenu = function (e) {
e.preventDefault()
alert('Right Click')
}
<h1>Please Right Click here!</h1>
Handle event using jQuery
library
$(window).on("contextmenu", function(e)
{
alert("Right click");
})