Fragmented Thought

Asynchronous Javascript Loading

Or, how to avoid third parties slowing down your site loads

By

Published

Lance Gliser

Heads up! This content is more than six months old. Take some time to verify everything still works as expected.

Read a good article today at http://engineeredweb.com/blog/non-blocking-script-loading-drupal/. It's a Drupal article, but really, it's good practice to avoid third party scripts linked in the body no matter the platform. Here's the code:

<script type="text/javascript"> var sNew = document.createElement("script"); sNew.async = true; sNew.src = "http://example.com/file.js"; var s0 = document.getElementsByTagName("script")[0]; s0.parentNode.insertBefore(sNew, s0); </script>

Just drop it near the body's closing tag.