2

I was reading through Which browsers support script async which is discussing Google's script which looks like this:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'xxx']);
_gaq.push(['_trackPageview']);

(function () {
    var ga = document.createElement('script');
    ga.type = 'text/javascript';
    ga.async = true;
    ga.src = 'https://ssl.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(ga, s);
})();

In the SO post, it states:

The async support as specified by google is achieved using two parts:

using script on your page (the script is supplied by google) to write out a <script> tag to the DOM.

that script has async="true" attribute to signal to compatible browsers that it can continue rendering the page.

The first part works on browsers without support for <script async.. tags, allowing them to load async with a "hack" (although a pretty solid one), and also allows rendering the page without waiting for ga.js to be retrieved.

The second part only affects compatible browsers that understand the async html attribute

This has me confused. What is the async attribute doing on the script above if they're already using another 'hack' to provide async support for browsers which do not respect the async: true attribute? Doesn't that hack work for all browsers?

3
  • Is type even recommended any more? I would say not Commented Oct 6, 2014 at 20:06
  • @danwellman: Yes, it is. What you might remember to be deprecated is language. Commented Oct 6, 2014 at 20:07
  • Nope. type has also been deprecated since HTML5. See this SO question, which was answered 4 years ago: stackoverflow.com/questions/4195427/… Commented Oct 7, 2014 at 7:58

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.