onClick with input type=“image”

匿名 (未验证) 提交于 2019-12-03 08:54:24

问题:

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); })(); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!