可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have been going round and round trying to find a definitive answer. Basically, I want to add the Google Analytics event tracking to a form submit - I was hoping to add an onClick to it, but I am having a tough time finding out if all the browsers support it.
I asked the person who designed the form, and she said that the reason they didn't use the button type is that is caused issues submitting the form in some browsers, but could not remember which ones.
So, will it work across browsers, or should I switch to button?
Relevant code below:
<input name="submit" id="submitme" type="image" src="/graphics/creative/landing_pages/popup/ibd_signmeupbtn.gif" alt="Sign Me Up!" onClick="_gaq.push(['_trackEvent', 'Registrations', 'Register', 'Lightbox']);" tabindex="8" />
EDIT I have _gaq defined. The real goal of this question is to make sure that using an onClick with an input image is OK to do.
回答1:
Try wiring it up a little different
<input name="submit" id="submitme" type="image" src="/graphics/creative/landing_pages/popup/ibd_signmeupbtn.gif" alt="Sign Me Up!" onclick="SignMeUp();" tabindex="8" />
And create your function like so:
<script type="text/javascript"> function SignMeUp() { if (!!_gaq) // make sure _gaq is defined _gaq.push(['_trackEvent', 'Registrations', 'Register', 'Lightbox']); } </script>
回答2:
you dont need to define the _gaq variable but you do need to import the google script.
here is an example of my code
var _gaq = _gaq || []; // i'm using asp.net web forms and getting google analytics code from config _gaq.push(['_setAccount', '<%= ConfigurationManager.AppSettings["google.analytics.code"] %>']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();