Mazen
Mazen

Reputation: 213

HTML onClick doesn't fire on rightclick

I've read that this is a problem in some browsers, but I'm still looking for a suitable solution.

My thoughts so far:

I could use the onmousedown/onmouseup event to distinguish between left and right mouse button. Since these events are called regardless of which button is pressed.

Problem with this is, that I only want to fire onRightClick event if the button was being pushed AND released above the same object. Not if the mouse button is pressed above the object and release else where (and vice versa).

Are there any workarounds which doesn't need complex code? like saving the clicked object in a global variable in onmousedown, and compare it to the object on which the onmouseup was fired. This seems unnecessary complex.

Edit: I forgot to mention that I'm using the JavaFX Webview, which doesn't seem to support the oncontextmenu event on SVG-Elements, which I need to use.

Upvotes: 0

Views: 1340

Answers (2)

Akshat G
Akshat G

Reputation: 214

You can use mousedown event with target parameter with outerHTML attribute. Here's a demo :

http://jsfiddle.net/qjffdptq/

You can modify as per need. Hope this helps.

Upvotes: 0

George Penchev
George Penchev

Reputation: 92

You can attach 'contextmenu' event listener of the element you want. When you execute your code return false to prevent the standard context menu from appearing.

element.addEventListener('contextmenu', function(event) {
    event.preventDefault();
    Foo();
    return false;
}, false);

You must also return false in the function you are calling internally.

function Foo(){
    // some code 
    return false;
}

Upvotes: 2

Related Questions