right click javascript code example

Example 1: disable right click javascript

//Disable mouse right click
$("body").on("contextmenu", function(e) {
  return false;
});

Example 2: javascript detect right click

el.addEventListener('contextmenu', function(ev) {
    ev.preventDefault();
    alert('success!');
    return false;
}, false);

Example 3: how to add a right click listener javascript

element.addEventListener('contextmenu', (e) => {
	e.preventDefault();
    alert('Success');
}, false);

Example 4: javascript detect right click

document.body.onclick = function (e) {
    var isRightMB;
    e = e || window.event;

    if ("which" in e)  // Gecko (Firefox), WebKit (Safari/Chrome) & Opera
        isRightMB = e.which == 3; 
    else if ("button" in e)  // IE, Opera 
        isRightMB = e.button == 2; 

    alert("Right mouse button " + (isRightMB ? "" : " was not") + "clicked!");
}

Example 5: javascript event right click

function rightclick() {
    var rightclick;
    var e = window.event;
    if (e.which) rightclick = (e.which == 3);
    else if (e.button) rightclick = (e.button == 2);
    alert(rightclick); // true or false, you can trap right click here by if comparison
}

Tags:

Php Example