Wuhtzu Posted February 12, 2011 Share Posted February 12, 2011 Hi everyone I am playing around with Google Analytics asynchronous tracking code and facing a "problem" where I have to have strings within strings in java script called by event handlers such as onClick. This piece of java script needs to be called when the even handler onBeforeUnload fires: onbeforeunload="timeTracker._recordEndTime(); _gaq.push(['java script code']);" So _gaq.push(['java script code']) takes a string of java script code and enqueues it for execution. The piece of codes needing to be executed is this: function(){ eTr = _gat._getTrackerByName('eventTracker'); timeTracker._track(eTr, undefined, document.title);} As you can see the code above function(){} has a string in it. How should those two pieces of code be merged so that everything gets interpreted correct? onbeforeunload="timeTracker._recordEndTime(); _gaq.push(['function(){ eTr = _gat._getTrackerByName('eventTracker'); timeTracker._track(eTr, undefined, document.title);}']);" I cannot mix " and ' since the html tags use ". The above with 'code 'string' more code' does (obviously) not work and escaping the quotes \' does not work either. Any suggestions? Best regards Wuhtzu Quote Link to comment https://forums.phpfreaks.com/topic/227442-js-strings-in-strings-in-event-handlers-such-as-onclick/ Share on other sites More sharing options...
.josh Posted February 12, 2011 Share Posted February 12, 2011 why don't you put all that in a function and call the function in the onbeforeunload? Quote Link to comment https://forums.phpfreaks.com/topic/227442-js-strings-in-strings-in-event-handlers-such-as-onclick/#findComment-1173285 Share on other sites More sharing options...
Wuhtzu Posted February 12, 2011 Author Share Posted February 12, 2011 I guess you are right, that might prevent some of the string in string problems. I'll try that and get back to you Quote Link to comment https://forums.phpfreaks.com/topic/227442-js-strings-in-strings-in-event-handlers-such-as-onclick/#findComment-1173291 Share on other sites More sharing options...
Wuhtzu Posted February 14, 2011 Author Share Posted February 14, 2011 What I needed I found documented here: http://code.google.com/apis/analytics/docs/tracking/asyncUsageGuide.html#PushingFunctions The function just has to be passed to _gaq.push() like a function and not a string: onbeforeunload="timeTracker._recordEndTime(); _gaq.push([function(){ eTr = _gat._getTrackerByName('eventTracker'); timeTracker._track(eTr, undefined, document.title);}]);" Quote Link to comment https://forums.phpfreaks.com/topic/227442-js-strings-in-strings-in-event-handlers-such-as-onclick/#findComment-1174177 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.