Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Defer parsing of JavaScript Magento
 
katsuchiyo
Jr. Member
 
Total Posts:  4
Joined:  2012-05-31
 

Hello all,

I want to defer parsing my javascript because but I tried many posts/tutorials on the internet and I really can’t fix it. I looked into the page.html file in the layout folder, but things I did didn’t work.

The javascript is a combined javascript http://minipccenter.nl/media/js/d017be718f4815629239fdbc924c341b.js

Can someone please tell me how to defer parsing it?

Yours,

Simon

 
Magento Community Magento Community
Magento Community
Magento Community
 
chiefair
Mentor
 
Avatar
Total Posts:  1848
Joined:  2009-06-04
 

http://www.magentocommerce.com/boards/viewreply/384445/
http://www.magentocommerce.com/boards/viewreply/388664/

 
Magento Community Magento Community
Magento Community
Magento Community
 
katsuchiyo
Jr. Member
 
Total Posts:  4
Joined:  2012-05-31
 

Thanks for your reply!

I’ve read both links already, but I really can’t figure out where I have to put it the text.
Could you tell me in which file I have to put the code? Because i tried it in page.html but ended up without result :(

 
Magento Community Magento Community
Magento Community
Magento Community
 
RahulTyagi
Member
 
Total Posts:  35
Joined:  2013-09-12
 

Hi,

To defer JS in magento, you need to override the function

getCssJsHtml()

that can be found in

Mage_Page_Block_Html_Head

In this function there is a code section as below:

// static and skin javascripts
            $html .= $this->_prepareStaticAndSkinElements('<script type="text/javascript" src="%s"%s></script>' "\n",
                empty(
$items['js']) ? array() : $items['js'],
                empty(
$items['skin_js']) ? array() : $items['skin_js'],
                
$shouldMergeJs ? array(Mage::getDesign(), 'getMergedJsUrl') : null
            
);

just override this function and replace this piece of code with the following

// static and skin javascripts
            $html .= $this->_prepareStaticAndSkinElements('<script type="text/javascript" defer src="%s"%s></script>' "\n",
                empty(
$items['js']) ? array() : $items['js'],
                empty(
$items['skin_js']) ? array() : $items['skin_js'],
                
$shouldMergeJs ? array(Mage::getDesign(), 'getMergedJsUrl') : null
            
);

Hope this helps.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Northern
Jr. Member
 
Avatar
Total Posts:  4
Joined:  2014-02-16
 

Hi, this solution will block functionality in admin panel cool hmm

 
Magento Community Magento Community
Magento Community
Magento Community
 
RahulTyagi
Member
 
Total Posts:  35
Joined:  2013-09-12
 

Hi,

Agreed that this will cause issues in Admin, but you can always skip something for Admin by putting a check like

<?php
    
if (Mage::app()->getStore()->isAdmin()) {
             
// your code there
    
}
    ?>

Hope it solves your admin issue.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Northern
Jr. Member
 
Avatar
Total Posts:  4
Joined:  2014-02-16
 

Thanks RahulTyagi, that did definitely worked for me.  smile

// static and skin javascripts
            $html .= $this->_prepareStaticAndSkinElements('<script type="text/javascript" defer src="%s"%s></script>' "\n",
                empty(
$items['js']) ? array() : $items['js'],
                empty(
$items['skin_js']) ? array() : $items['skin_js'],
                
$shouldMergeJs ? array(Mage::getDesign(), 'getMergedJsUrl') : null
            
);
            
            
// If Mage is Admin
            
if (Mage::app()->getStore()->isAdmin()) {
                $html 
.= $this->_prepareStaticAndSkinElements('<script type="text/javascript"  src="%s"%s></script>' "\n",
                empty(
$items['js']) ? array() : $items['js'],
                empty(
$items['skin_js']) ? array() : $items['skin_js'],
                
$shouldMergeJs ? array(Mage::getDesign(), 'getMergedJsUrl') : null
            
);
            
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
Northern
Jr. Member
 
Avatar
Total Posts:  4
Joined:  2014-02-16
 

RahulTyagi, After testing admin panel i conclude that to avoid more issues better use async in place of defer with the same code.

// static and skin javascripts
            $html .= $this->_prepareStaticAndSkinElements('<script type="text/javascript" async src="%s"%s></script>' "\n",
 
Magento Community Magento Community
Magento Community
Magento Community
 
baraksteve
Jr. Member
 
Total Posts:  4
Joined:  2012-11-19
 

I changed /app/code/core/mage/page/block/html/head.php as suggested here and I flushed the cache.

When I run http://gtmetrix.com it still shows that my score is F (4) on the Defer parsing of JavaScript line.

Am I doing something wrong? I tried defer and async both same results.

In the browser I do feel that pages are loading faster though....

Thanks,

Steve

 
Magento Community Magento Community
Magento Community
Magento Community
 
Northern
Jr. Member
 
Avatar
Total Posts:  4
Joined:  2014-02-16
 

Go to /app/code/core/Mage/Page/Block/Html look at line +-203 for // static and skin javascripts
Replace with this:

// static and skin javascripts
            $html .= $this->_prepareStaticAndSkinElements(\'<script type=\"text/javascript\" async src=\"%s\"%s></script>\' \"\\n\",
                empty(
$items[\'js\']) ? array() : $items[\'js\'],
                empty(
$items[\'skin_js\']) ? array() : $items[\'skin_js\'],
                
$shouldMergeJs ? array(Mage::getDesign(), \'getMergedJsUrl\') : null
            
);
            
               
// If Mage is Admin
            
if (Mage::app()->getStore()->isAdmin()) {
                $html 
.= $this->_prepareStaticAndSkinElements(\'<script type=\"text/javascript\"  src=\"%s\"%s></script>\' \"\\n\",
                empty(
$items[\'js\']) ? array() : $items[\'js\'],
                empty(
$items[\'skin_js\']) ? array() : $items[\'skin_js\'],
                
$shouldMergeJs ? array(Mage::getDesign(), \'getMergedJsUrl\') : null
            
);
            
}

You get for sure your js parsed but you may get blocked js functionality...clean cache an renew it, go to gtmetrix and test it again. (Y)

 
Magento Community Magento Community
Magento Community
Magento Community
 
krnekdo
Jr. Member
 
Total Posts:  1
Joined:  2011-06-13
 

This doesn’t work for me.. It messes up my sliders.. Shows all pictures from the sliders at once.. Does this mean that I can’t defer JS if I have sliders and carousels on my site?

Thanks!

Northern - 11 June 2014 03:39 AM

Go to /app/code/core/Mage/Page/Block/Html look at line +-203 for // static and skin javascripts
Replace with this:

// static and skin javascripts
            $html .= $this->_prepareStaticAndSkinElements(\'<script type=\"text/javascript\" async src=\"%s\"%s></script>\' \"\\n\",
                empty(
$items[\'js\']) ? array() : $items[\'js\'],
                empty(
$items[\'skin_js\']) ? array() : $items[\'skin_js\'],
                
$shouldMergeJs ? array(Mage::getDesign(), \'getMergedJsUrl\') : null
            
);
            
               
// If Mage is Admin
            
if (Mage::app()->getStore()->isAdmin()) {
                $html 
.= $this->_prepareStaticAndSkinElements(\'<script type=\"text/javascript\"  src=\"%s\"%s></script>\' \"\\n\",
                empty(
$items[\'js\']) ? array() : $items[\'js\'],
                empty(
$items[\'skin_js\']) ? array() : $items[\'skin_js\'],
                
$shouldMergeJs ? array(Mage::getDesign(), \'getMergedJsUrl\') : null
            
);
            
}

You get for sure your js parsed but you may get blocked js functionality...clean cache an renew it, go to gtmetrix and test it again. (Y)
 
Magento Community Magento Community
Magento Community
Magento Community
 
baraksteve
Jr. Member
 
Total Posts:  4
Joined:  2012-11-19
 

Yeah, me too, after replacing the code with your suggestion the entire front and back end show blank screens.

Thanks,

 
Magento Community Magento Community
Magento Community
Magento Community
 
RahulTyagi
Member
 
Total Posts:  35
Joined:  2013-09-12
 

Hi,

Please check the PHP code again, you might have left syntax error in it.

Most of the times Page goes blank when there is a syntax error in PHP code. As this file runs for both Frontend and Admin you are seeing a blank screen.

Thanks.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top