How can I defer parsing of Javascript on my Drupal 7 site? I am working on some performance issues for a site I launched recently.  Looking at Google's PageSpeed insights I see that High priority on the list is Defer parsing of Javascript. Here is a link:

https://developers.google.com/speed/pagespeed/insights#url=http_3A_2F_2Fwww.gastonia.com_2F&amp;mobile=false&amp;rule=DeferParsingJavaScript

Wow, over 1 MB of Javascript to parse!! Notice that most of these are from the facebook app (maybe the answers is to remove the fb app, ha!)

Has anyone successfully deferred this in Drupal?  I have tried using script defer="defer", and Google gives suggestions like "add script tags in comments and then eval() them, but they give no examples and I am having trouble finding a good tutorial.  Any resources or direction is appreciated!

On a side note, if you see anything else in that list that is easily overcomable by drupal, please add a comment. The site in question is http://www.gastonia.com in case you wanted to look at the source code.
Given that nearly everything in Javascript for Drupal is triggered by a $(document).ready() in jQuery, I don't see any real advantage to using defer.  In fact, I recently had a real headache1 from some code I inherited that mixed the two.

One recommendation (I think YSlow suggests it) that you can do is to move scripts to the end, right before the closing &lt;/body&gt;.  This would mean moving the &lt;?php print $scripts; ?&gt; in your html.tpl.php.  While not deferring the parsing, it does delay the loading so your page renders out quicker, and I have never had any problems in Drupal doing this.

If you do want to add the defer, you need to implement hook_js_alter:

function mymodule_js_alter (&amp;$javascript)
{
  $javascript['misc/drupal.js']['defer'] = TRUE;
}


If you want to defer everything, you could try

function mymodule_js_alter (&amp;$javascript)
{
  foreach ($javascript as $key =&gt; $value) {
    $javascript[$key]['defer'] = TRUE;
  }
}


Otherwise, most of the normal Pagespeed fixes don't have much to do with Drupal itself.  As long as you deploy with JS and CSS aggregation enabled you should be good.  Configure Apache to use compressed responses and decent cache lifetimes and I don't think PageSpeed will complain about much and give you all A's and B's.