title: Coupling/chaining multiple asynchronously loaded JS scripts (JavaScript)
body: <h3>Problem description:</h3>
  
  <p>I'm working on a project to take an existing JavaScript script and modify it to support being loaded asynchronously. If you don't know about asynchronous or lazy loading scripts but want to learn more <a href="http://friendlybit.com/js/lazy-loading-asyncronous-javascript/" rel="nofollow noreferrer">read this</a>. </p>
  
  <p>One of the first things our main.js script does is call our web-server (3rd party to our clients) to get some dynamic data. Which is the problem because that data is return via a dynamically added JS file (data.js), which contains variables that are needed in other parts of main.js. The problem is similar to the issue described in chapter 5 of the book <a href="http://books.google.com/books?id=E7p-07kNfXYC&amp;printsec=frontcover&amp;dq=Even+Faster+Web+Sites&amp;source=bl&amp;ots=ULipSGfeQr&amp;sig=GjCWGok0n2CDA_mcumPtAUIx2xk&amp;hl=en&amp;ei=5I5ITMatHIOosQPWo9FI&amp;sa=X&amp;oi=book_result&amp;ct=result&amp;resnum=7&amp;ved=0CDwQ6AEwBg#v=onepage&amp;q&amp;f=false" rel="nofollow noreferrer">Even Faster web sites</a>.  Which deals with coupling inline scripts with async scripts.  Anyway...  The problem being that main.js doesn't know when data.js has finished loading...</p>
  
  <p>Currently main.loadDataJs() uses the "<em>Document.write Script Tag</em>" technique to dynamically load our JS file, but this doesn't work for async scripts (for anyone who hasn't tried it: it results in a blank page after loading).  So, we have to use the "<em>Script Dom Element</em>" technique of loading data.js dynamically.  The problem here is figuring out when the browser has downloaded and executed the added script node.  So I'm looking for a method of coupling data.js with the async script main.js.</p>
  
  <p>My priority is to build something that is reliable on both PC browsers and mobile browsers. It has to have a very small and light weight footprint, because the amount of data being returned by data.js is pretty small (about 5-10 key/value pairs). It has to support 3rd party communication. </p>
  
  <p><br/><hr/> </p>
  
  <h3>Solutions I'm investigating:</h3>
  
  <ul>
  <li>From my own research, I'm leaning towards modifying the server code that serves up data.js to support JSONP.  That is: when we set the src attribute of the script object the call would have a callback parameter <code>src="data.js?callback=fooCallback"</code> that causes the data to be wrapped in a function call, like so <code>fooCallback( { geoC :  "US" } );</code>.  From my experimenting this seems to be the most reliable solution, since it isn't browser dependent.  Deploying server-side code means greatly increasing the scope of the project, but C'est la vie.</li>
  </ul>
  
  <p>I also experimented with:</p>
  
  <ul>
  <li><p>Using a polling Timer.  Which checks for the data to exist and if so call the "callback" function, else calls setTimeout again. My problem with this solution is that it isn't event driven so it doesn't respond as quickly as I like, and it causes the browser's busy indicator to activate.  So if data.js takes a long time to download or fails, it could "look" busy for a long time.</p></li>
  <li><p>Using the script block's onload and onreadystatechange handlers.  This would have been a perfect solution except for two issues I hit.
  <br><strong>A)</strong> On a fast machine using IE8, I was able to get the onreadstatechange to fire the <code>s.readyState === "loaded"</code> event before it had executed data.js, I assume it was downloaded but still being executed :-/.  I was able to fix this by adding a <code>setTimeout(fooCallBack, 1);</code> call inside the onreadystatechange handler.  But I'm not a 100% happy with that solution.
  <br><strong>B)</strong> I have read (but haven't tested yet) that this technique doesn't work on IE6 minus. Any comments? </p></li>
  </ul>
  
  <p>I looked into other non-dynamic-JavaScript solutions but they all seemed heavier handed than I was looking for.  Like the <a href="http://msdn.microsoft.com/en-us/architecture/bb735305.aspx" rel="nofollow noreferrer">iframe</a> or flash techniques used by Facebook and others.</p>
  
  <p><br/><hr/></p>
  
  <h3>My Questions:</h3>
  
  <p>From anyone who has coupled async scripts together, I'm curious what solutions you choose and how reliable you have found them?</p>
  
  <p>Is there some alternative solution I haven't talked about? </p>
  
  <p>Any general comments on anything I've talked about?</p>
  
  <p><br/><hr/></p>
  
  <h3>Definitions:</h3>
  
  <p>The definitions of the "techniques" are borrowed from Steve Souders's book <a href="http://books.google.com/books?id=E7p-07kNfXYC&amp;printsec=frontcover&amp;dq=Even+Faster+Web+Sites&amp;source=bl&amp;ots=ULipSGfeQr&amp;sig=GjCWGok0n2CDA_mcumPtAUIx2xk&amp;hl=en&amp;ei=5I5ITMatHIOosQPWo9FI&amp;sa=X&amp;oi=book_result&amp;ct=result&amp;resnum=7&amp;ved=0CDwQ6AEwBg#v=onepage&amp;q&amp;f=false" rel="nofollow noreferrer">Even Faster web sites</a>. A great book BTW!</p>
  
  <p><em>script dom element</em>  – a method of adding a dynamic script by creating a script element and setting its src property to the script’s URL.</p>
  
  <p><em>document.write script tag</em> – a method of adding a dynamic script by writing the  HTML into the page using document.write.</p>
  
  <p><br></p>