views:

500

answers:

3

Hello,

I am working on a web app, in which I want to have a different action to happen to an element whether I left or right click on it

So I added a function for "click" with jQuery and added a function for the "oncontextmenu" attribute of my element.

This is working well in Chrome & IE but raises a problem in Firefox: when I right click on an element, the left click event is raised first so both my left then right click functions are called.

How can I make Firefox not calling the left click function when I right click?

Thanks for your help.

A: 

If you're going with jQuery I believe this is something you have to add in. See http://abeautifulsite.net/notebook/68 for an example.

annakata
+4  A: 

Yeah, browsers traditionally send right-clicks to the onclick handler, with the event.which property set to 3 instead of 1. IE used oncontextmenu instead, then Firefox picked up oncontextmenu in addition to the usual onclick. To cater for the browsers you will have to catch both events — or find a plugin that will do it for you.

Note that even with this sorted out, you are still not guaranteed to get right click events or be able to disable the standard context menu. Because many web pages abused the ability, it is disablable in many browsers, and sometimes disabled by default (eg. in Opera). If your app provides right-click actions, always ensure there is an alternative way to bring them up.

bobince
+1  A: 

Hello,

Thanks for your help.

My problem came from the fact that on one side I was using the insanely great jquery "live" function for "click" and "oncontextmenu" attribute on the other. (using onclick and oncontextmenu was not a problem).

I've just modified my $.live("click"...) function by catching the event and not firing the rest when e.which is 3.

Problem solved!

Thanks again.

PS: Ok, I will stop crazily punctuating my Qs ;)) Thought was a good way to bring attention to amazing devs ;)