I am using magento 1.9 version. I want to speed up my magento site, when I did gtmetrix analysis, it shows Defer parsing of JavaScript. Js files are already minified. How to do it.



Originally answered here.

Main idea here is to move all javascript to the bottom.

Create observer on http_response_send_before:


In Observer.php move all javascript to the bottom:

public function httpResponseSendBefore($observer)
  if(!Mage::helper('goivvy_deferjs')->isEnabled()) return;
  $response = $observer->getEvent()->getResponse();
  $html = $response->getBody();
  preg_match_all('#(<script.*?</script>)#is', $html, $matches);
  $js = '';
  foreach ($matches[0] as $value)
    $js .= $value;
  $html = preg_replace('#<script.*?</script>#is', '', $html);
  $html = preg_replace('#</body>#',$js.'</body>',$html);

Here is the already made paid extension (for Magento 1 and Magento 2).


To defer JavaScript to make you site load faster, you need to do some things that delay JavaScript loading until the browser finish loading HTML and CSS or external sources.

Because when the browser processes a web page, it will load based on an order, from the head, body to bottom. Therefore, it will load all HTML, CSS and JS together. This is the reason why your site performs slower. To defer JavaScript, you can look for some extensions that can help you solve more simply. On Magento Connect, there is extension Defer JavaScript, I think it is useful in your case: https://www.magentocommerce.com/magento-connect/defer-javascript.html It said that it can defer Javascript and also Iframes to reduce loading time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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