0

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.

GTMetrix

2
  • Please refer stackoverflow.com/a/19558385/1409718 Commented Apr 17, 2017 at 11:28
  • Thanks @JaiminSutariya but the js is already minified, how can i specify there?.(ie., if i enable/disable minify jss/css it will change) Commented Apr 17, 2017 at 13:49

2 Answers 2

1

Originally answered here.

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

Create observer on http_response_send_before:

<frontend>
<events>
<http_response_send_before>
   <observers>
      <goivvy_deferjs_http_response_send_before>
          <class>goivvy_deferjs/observer</class>
          <type>singleton</type>
          <method>httpResponseSendBefore</method>
      </goivvy_deferjs_http_response_send_before>
   </observers>
</http_response_send_before>
</events> 
</frontend>

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);
  $response->setBody($html);
}

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

0

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 and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.