This question already has an answer here:

gtmetrix.com suggest I defer parsing of 35 js files to increase page load time. I have 2 questions. I am very new to this and a little lost, so apologise in advance if I shouldn't be here asking such simple things.

  1. Is there an easy way to know which js should be delayed for the quickest page load time. Which will not affect functionality? I have only identified 5 js files currently which I would be fairly confident would not affect the main page loading.

  2. I am also unsure which file/files I should place the following code into to defer the javascript. (I know, I have huge gaps in my knowledge) Will the files be unique to my site, or is it the htaccess fie or a generic file that will be the same regardless of the installation. I am completely lost.

source: http://www.feedthebot.com/pagespeed/defer-loading-javascript.html

<script type="text/javascript">
function downloadJSAtOnload() {
var element = document.createElement("script");
element.src = "defer.js";
if (window.addEventListener)
window.addEventListener("load", downloadJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJSAtOnload);
else window.onload = downloadJSAtOnload;

marked as duplicate by Fabian Schmengler, Keyul Shah, Matthias Zeis, Sander Mangel, Amit Bera Nov 13 '14 at 7:10

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • While the question itself isn't an exact duplicate I think the link above will explain where the problems are with deferring the load. – Matthias Zeis Nov 13 '14 at 6:14

I recommend installing (or asking your web host to install) mod_pagespeed on your server. It can defer javascript globally and you don't need to change Magento to achieve it at all. Also mod_pagespeed contains many other optimisations you are probably looking for if you're interested in page load time.

  • This answer is specific to apache2 webserver, and doesn't really help matters in general (nginx, ISS etc). While i do use google labs PageSpeed on my NginX server, you have to be rather careful about it's javascript options, they can lead to very unexpected and unwanted results. For example , in their deffer JS code, they inject '<noscript><meta HTTP-EQUIV="refresh" content="0;' into the page body tag. If you the client browser doesn't support JS. This can lead to client warnings with bad/out of date anti-virus / all in one protection browser plugins from bad security software. – Alistair Nov 13 '14 at 1:50
  • This didn't work for me even though it gave me a better score on gtmetrix.com it added an extra 2 seconds to my load time. I would much rather tweak everything manually as I am a firm believer that the more mods you add the more time it will take to load and run those mods. – jmituzas Jan 24 '15 at 17:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.