Search code examples
javascripthtmldompageload

<script async > vs document.getElementsByTagName('script')[0].insertBefore etc.?


Is there any material difference between:

<script type="text/javascript" src="/script.js" async></script>

and

(function() {
  var s = document.createElement('script'); 
  s.type = 'text/javascript'; 
  s.async = true; 
  s.src = 'script.js'; 
  var x = document.getElementsByTagName('script')[0]; 
  x.parentNode.insertBefore(s, x)
;})();

?


Solution

  • I can only quote MDN:

    In older browsers that don't support the async attribute, parser-inserted scripts block the parser; script-inserted scripts execute asynchronously in IE and WebKit, but synchronously in Opera and pre-4.0 Firefox.

    So in other words, the second one will still evaluate the script asynchronously in older IE and WebKit browsers.