0

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.

2
  • @Simeon, what's the lengthy explanation? A one word eval is enough. Commented Oct 24, 2011 at 12:08
  • @Blindy, based on the wording in the question I'd think that arrays, variables and script tags will have to be explained a bit Commented Oct 24, 2011 at 12:12

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this combined with snook.ca/archives/javascript/global_variable gave me my answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.