If you're using the new jQuery.live("type", fn) handlers in jQuery 1.3, be careful with the "click" event type. Unlike jQuery.bind("click", fn) and jQuery.click(fn), jQuery.live("click", fn) doesn't distinguish between mouse buttons. So while the "normal" handlers will only trigger when the left button is used, the live handler will trigger on any button.
The workaround is pretty simple, though kind of kludgey: add the stuff in bold to your handler.
jQuery("#selector").live("click", function(event) { if (event.button != 0) { // wasn't the left button - ignore return true; } // do some stuff return false; // "capture" the click });
Hopefully this will be addressed in the future, but for right now you have to pick between manual button detection and sticking with the "normal" handlers.
nice find, Barney. Thanks
NOTE: Internet Explorer treats left-click events as button 1. FF/Safari, etc all treat it as button 0.
nirvana,
If that's the case (I don't know, offhand), jQuery is isolating me (the developer) from it perfectly. Hit http://barneyb.com/r/jquery_live_button.cfm in whatever browser you want for a demo. FF and IE both return zeros for both live and click events.
The 'button' event attribute is not normalized by jQuery. But it does normalize event.which to be 1,2,3 depending on the button clicked (1=left,2=middle,3=right).
So use event.which instead of event.button.
event.which is returning undefined in IE7…
same behaviour with the third mouse button.
Is this script possible to detect Adsense click?
@Resourcednet
No, you can't detect Adsense clicks; they're loaded in an IFRAME with a document from a different domain which is consequently inaccessible from your page's JS.
How about this? http://www.bennadel.com/blog/1752-Tracking-Google-AdSense-Clicks-With-jQuery-And-ColdFusion.htm
@Resourcednet
Yes, that would let you synthesize when clicks may happen based on mouse movements and focus changes, but it's not detecting Adsense clicks. Ben calls out several places where it'll fail to do what you want, not to mention it doesn't tell you anything about the supposed click (e.g., the target), just that one probably happened.
I love searching for bug reports and finding you :-)
I learned something on a project I was working on the other day. I was having trouble figuring out whether to use the which event or the button event when trying to detect what button was clicked in different browsers. Using the button event values returned inconsistent results. Using the which event value was more consistent but IE always returned it as undefined/null. Here is what I did to make it work for me.