0
votes

I'm developing a javascript widget, and I need to send parameters to Javascript, or be able to access variables defined within SCRIPT tag of an HTML. For example, Google Analytics. The GA code below makes variables like _setAccount and _trackPageview available to the javascript, and I need to do the same. How can I achieve that? How can the called javascript https://ssl.google-analytics.com/ga.js or http://www.google-analytics.com/ga.js in this case access the defined variables?

<script type="text/javascript"> 
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXX-1']);
_gaq.push(['_trackPageview']);
(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);
})();    
</script>

Any help is highly appreciated.

TIA, James.

1
@Simeon, what's the lengthy explanation? A one word eval is enough.Blindy
@Blindy, based on the wording in the question I'd think that arrays, variables and script tags will have to be explained a bitSimeon

1 Answers

1
votes

The Google code that's pulled in via the added <script> tag simply expects to be able to access a global (window) variable named "_gaq", and it expects it to be an array with particular properties, etc.