22
Nov
2010
Grayside

How to Use jQuery to Smoothly Add a Script After Page Load

A colleague of mine slapped this trick together and I wanted to jot it down:

  $script_url = "http://www.example.com/script.js";
 
  $my_js = "
  $(document).ready(function(){
        jQuery.getScript('".$script_url."');
        });";
 
  drupal_add_js($my_js, 'inline');

It waits until the page has loaded, then it goes and inline includes an external javascript. The site was loading half-way, then spending a couple seconds processing a bunch of externally-sourced javascript. Now it’s effectively seamless.