jQuery-Google-Analytics icon indicating copy to clipboard operation
jQuery-Google-Analytics copied to clipboard

When URL is provided in href, .preventDefault() gets clobbered

Open brob opened this issue 12 years ago • 4 comments

When I have a click event and preventDefault on the event, if it has event tracking, it no longer prevents the link from going through. it registers the event, but my javascript no longer runs properly.

This does NOT happen when # is provided for the href.

brob avatar Oct 17 '13 17:10 brob

Hi there,

Thanks for that. I'm struggling to follow what you mean. Could you perhaps post a code sample for me to follow so I can see what is going wrong first hand?

Many Thanks! :)

JimBobSquarePants avatar Oct 17 '13 21:10 JimBobSquarePants

My use was tracking events on something that had a javascript function that did some show/hide stuff, but has a fallback URL in the href of the anchor so that it still does something if JS fails for one reason or another. If you put a real URL in the anchor, the plugin sees it, intercepts it and takes the user to that page (ignoring the e.preventDefault in my code). This looks to be because it's checking to see if it's NOT a # and if it's not, it runs the intercept to add the timeout and then sends the browser to the url.

I'll add a code example later today if I can.

brob avatar Oct 18 '13 13:10 brob

Here's a gist with an html page: https://gist.github.com/brob/3e244abd2b583773318e

It's very very simple. My use case has a more complex function than just a console log, but i hope it helps illustrate the point.

In my fix i'm passing a data attribute (data-ga-nopass="true") into my anchor to stop the default behavior of the plugin

brob avatar Oct 18 '13 15:10 brob

Oh yeah, that's really obvious now sorry. I'll get that implemented asap. Thanks.

JimBobSquarePants avatar Oct 24 '13 09:10 JimBobSquarePants