asynchronous .js file loading syntax

Posted by taber on Stack Overflow See other posts from Stack Overflow or by taber
Published on 2010-05-05T15:02:55Z Indexed on 2010/05/05 15:08 UTC
Read the original article Hit count: 341

Hi, I noticed that there seems to be a couple of slightly different syntaxes for loading js files asynchronously, and I was wondering if there's any difference between the two, or if they both pretty much function the same. I'm guessing they work the same, but just wanted to make sure one method isn't better than the other for some reason. :)

Method One

(function() {

var d=document, h=d.getElementsByTagName('head')[0], s=d.createElement('script'); s.type='text/javascript'; s.src='/js/myfile.js'; h.appendChild(s);

})(); /* note ending parenthesis and curly brace */




Method Two (Saw this in Facebook's code)

(function() {

var d=document, h=d.getElementsByTagName('head')[0], s=d.createElement('script'); s.type='text/javascript'; s.async=true; s.src='/js/myfile.js'; h.appendChild(s);

}()); /* note ending parenthesis and curly brace */

© Stack Overflow or respective owner

Related posts about javascript-execution

Related posts about JavaScript