Title: defer loading of javascript
Last modified: August 21, 2016

---

# defer loading of javascript

 *  Resolved [Hamed.T](https://wordpress.org/support/users/hamedt/)
 * (@hamedt)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/defer-loading-of-javascript-3/)
 * hi,
    can you add some options to add scripts and css with defer loading? like:
 *     ```
       <script type="text/javascript">
       function downloadJSAtOnload() {
       var element = document.createElement("script");
       element.src = "myscript.js";
       document.body.appendChild(element);
       }
       if (window.addEventListener)
       window.addEventListener("load", downloadJSAtOnload, false);
       else if (window.attachEvent)
       window.attachEvent("onload", downloadJSAtOnload);
       else window.onload = downloadJSAtOnload;
       </script>
       ```
   
 * it’s one of pagespeed recommendation!
 * ** and please do NOT remove CDN options from your plugin it’s why i use your 
   plugin!
 * thanks in advance
 * [http://wordpress.org/plugins/autoptimize/](http://wordpress.org/plugins/autoptimize/)

Viewing 7 replies - 1 through 7 (of 7 total)

 *  Plugin Author [Frank Goossens](https://wordpress.org/support/users/futtta/)
 * (@futtta)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/defer-loading-of-javascript-3/#post-4096173)
 * Thanks for your input Hamed.T, always refreshing to get these kind of tips (and
   good reviews)!
 * Regarding deferring CSS & JS; for CSS this [might cause “Flash Of Unstyled Content”](https://en.wikipedia.org/wiki/Flash_of_unstyled_content)
   which I presume most Autoptimize users would appreciate. JS is another issue;
   there “defer” might indeed make sense, but given that Autoptimize places the 
   link to the aggregated script-file at the very bottom of the HTML, the gains 
   will probably be minimal. Testing this is on my to-do list though, so it might
   indeed be added if it proves helpful.
 * As far as CDN is concerned; I will make sure to provide some guidance on how 
   to switch to alternate solutions when that functionality gets removed from Autoptimize.
 *  Thread Starter [Hamed.T](https://wordpress.org/support/users/hamedt/)
 * (@hamedt)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/defer-loading-of-javascript-3/#post-4096201)
 * i’m sure using defer for scripts can be useful coz it’s one of google pagespeed
   recommendation…
    i recently find a link when searching about defer and i think
   it’s can be useful, the author say css and js file loaded after page loading 
   if we use this script.. so here is the [link](http://www.mysqldotnet.com/siteoptimization-29)
   also i’m sure you do your best 🙂 thanks for respond
 *  Plugin Author [Frank Goossens](https://wordpress.org/support/users/futtta/)
 * (@futtta)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/defer-loading-of-javascript-3/#post-4096298)
 * A small update with regards to defer; defer on scripts that are at the end of
   the HTML (as Autoptimize’s, by default) does not have any effect, cfr. [this discussion on the HTML5 boilerplate development forum](https://github.com/h5bp/lazyweb-requests/issues/42#issuecomment-1779556)(
   hixie being [Ian Hickson](https://en.wikipedia.org/wiki/Ian_Hickson), the HTML5
   spec lead).
 *  Plugin Author [Frank Goossens](https://wordpress.org/support/users/futtta/)
 * (@futtta)
 * [12 years, 6 months ago](https://wordpress.org/support/topic/defer-loading-of-javascript-3/#post-4096373)
 * In 1.7.0 I ended up enabling this anyhow for aggregated javascript at the end
   of the HTML (so not if “force script in head” is set), as it does have a slight
   positive effect on the (mobile) pagespeed score 🙂
 *  Thread Starter [Hamed.T](https://wordpress.org/support/users/hamedt/)
 * (@hamedt)
 * [12 years, 6 months ago](https://wordpress.org/support/topic/defer-loading-of-javascript-3/#post-4096374)
 * that’s sound great thanks
 *  [belg4mit](https://wordpress.org/support/users/belg4mit/)
 * (@belg4mit)
 * [12 years, 5 months ago](https://wordpress.org/support/topic/defer-loading-of-javascript-3/#post-4096390)
 * > A small update with regards to defer; defer on scripts that are at the end 
   > of the HTML (as Autoptimize’s, by default) does not have any effect, cfr. this
   > discussion on the HTML5 boilerplate development forum (hixie being Ian Hickson,
   > the HTML5 spec lead).
 * Would it be possible to break the defer attribute out into a separate option?
   I like the script in the footer, but with defer various page elements do not 
   get rendered on first load. I’d have to exclude/noptimize many jQuery components
   in order to get it to work, but then I lose the advantage of having all the code
   bundled at the end.
 *  Plugin Author [Frank Goossens](https://wordpress.org/support/users/futtta/)
 * (@futtta)
 * [12 years, 5 months ago](https://wordpress.org/support/topic/defer-loading-of-javascript-3/#post-4096391)
 * I’ll probably not make this a separate option (trying to somewhat limit options,
   considering the impact on usability of having too many of those), but overriding“
   defer” for javascript at the bottom is something that would definitely be possible
   using a filter that would be part of a simple API I hope to have in 1.8 or 1.9.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘defer loading of javascript’ is closed to new replies.

 * ![](https://ps.w.org/autoptimize/assets/icon-256X256.png?rev=2211608)
 * [Autoptimize](https://wordpress.org/plugins/autoptimize/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/autoptimize/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/autoptimize/)
 * [Active Topics](https://wordpress.org/support/plugin/autoptimize/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/autoptimize/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/autoptimize/reviews/)

## Tags

 * [defer loading](https://wordpress.org/support/topic-tag/defer-loading/)

 * 7 replies
 * 3 participants
 * Last reply from: [Frank Goossens](https://wordpress.org/support/users/futtta/)
 * Last activity: [12 years, 5 months ago](https://wordpress.org/support/topic/defer-loading-of-javascript-3/#post-4096391)
 * Status: resolved