How to handle left and right click in Firefox

I am working on a web application in which I want some action to happen on an element, whether I left it or right-click on it.

So, I first added a function to handle the click event using jQuery, and then added a second function to handle the oncontextmenu attribute of my element.

This works well in Chrome and IE, but causes a problem in Firefox. When I right-click on an element, my function that processes the left click invokes an unexpected name, and then my function that processes the right click is called.

How can I make Firefox not invoke the left-click function when right-clicking?

+3
source share
2 answers

Yes, browsers traditionally send clicks onclick right clicks, and event.which assigns the value 3 instead of 1. IE used oncontextmenu instead, then Firefox took oncontextmenu in addition to the usual onclick. To serve browsers, you will have to catch both events - or find a plugin that does this for you.

Please note that even with this sorting, you still do not guarantee receiving events with a right click or the ability to disable the standard context menu. Since many web pages have abused the ability, it is not verifiable in many browsers and is sometimes disabled by default (for example, in Opera). If your application provides right-click actions, always make sure there is an alternative way to create them.

+6
source

My problem arose because on the one hand I used the insanely great jQuery live function for click and the oncontextmenu attribute on the other. (Using onclick and oncontextmenu not a problem).

I just changed my function $.live("click"...) , catching the event and not letting go of the rest when e.which is 3 .

The problem is solved!

+1
source

All Articles