BuySellAds and Google Analytics, in an attempt to make the internet faster, recently changed the code snippets they use for serving ads and tracking visitors, respectively, to be non-blocking and asynchronous. This means that the scripts won’t hold up the rendering of your pages while they load. If you reload this page, as an example, the ads to the right may actually appear after the rest of the page has finished loading. It gives the appearance of being a lot faster.
How can you load your own scripts asynchronously? Here’s an example I put together after dissecting Googles’ and BuySellAds’ scripts:
<script id="myscript" type="text/javascript"> (function() { var myscript = document.createElement('script'); myscript.type = 'text/javascript'; myscript.src = ('http://example.org/myscript.js'); var s = document.getElementById('myscript'); s.parentNode.insertBefore(myscript, s); })(); </script>
What it does is it dynamically assembles and writes out a <script type="text/javascript" src="..." />
DOM element, placing it just before the loader script. You could alter the insertBefore
part to append loaded script to the <head>
element if you’re so inclined.
You can read further on this technique from these sources: