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

What is ‘before_body_end’
 
Vince RS
Jr. Member
 
Total Posts:  22
Joined:  2010-10-27
 

Hi new to Magento and just trying to get my head around the structure, was just wondering what is being called with ‘before_body_end’ and is it integral to the design? Is it safe to delete?

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

One of two places you can insert javascript code.

after_body_start is the current insertion point for Google Analytics code running under the asynchronous tracker.

before_body_end is for other scripts that you want to load after the page has mostly rendered. Insertion can be done by putting the following in your local.xml and then creating a template block in a folder in your store templates. Be sure to name each added phtml block (name="”).

local.xml

<default>
            <
reference name="before_body_end">
                <
block type="core/template" name="trackgoober" template="tracker/javacode.phtml" />
            </
reference>
        </default>
yourtemplate/tracker/javacode.phtml
<!-- Start of Logging Code  -->
<
script type="text/javascript" language="javascript">
    ~ 
tracker code supplied by social disease network consultant here
</script>
<!-- End of Logging Code -->

Another way of dealing with annoying tracking code from slow servers (prototype 1.6 as shipped in 1.4.x.x) is to write a code link loader for external javascript to make the browser insert it after DOM load. So, instead of embarrassing your site by taking their code fungus and blindly sticking it in your html code like the following:

<script type="text/javascript" src="https://example.s3.amazonaws.com/amaze/loader/56b0af9d38123deadbeef8faedec.js"></script>

use prototype to load after dom assembly. This can be stuck anywhere and it dynamically adds the link into the end of the body at the proper time so the browser starts searching for slow code on slow servers after the html has been rendered.

<!-- Social Overlay Loader -->
<
script type="text/javascript">
//<![CDATA[
    
document.observe("dom:loaded", function() {
        
// load script after dom load
        
$$("body")[0].insert(new Element("script"{
          type
"text/javascript",
          
src"https://example.s3.amazonaws.com/amaze/loader/56b0af9d38123deadbeef8faedec.js"
        
}));
    
});
//]]>
</script>

A major reason for doing it “the right way through layouts” is that every so often these tracking mechanisms and the servers they call undergo profound failure. When that happens, you can have your website start loading partial pages, stutter from script blocking despite it supposedly being loaded asynchronously, have the cart itself actually fail to either load or the one page process Magento uses inexplicably lose panels, etc.

When this happens, disabling the tracker code sitewide is as simple as adding an HTML comment to the offending line in local.xml and clearing your cache.

<default>
        <
reference name="before_body_end">
            <!-- <
block type="core/template" name="trackgoober" template="tracker/javacode.phtml" /> -->
        </
reference>
    </default>

Naming each included html block is “a really good idea ™”. If you don’t want to completely lose the statistics, you can forgo commenting the line out and only eliminate the named html block during cart processing by adding the following to your local.xml.

<checkout_cart_index translate="label">
        <
reference name="before_body_end">
            <
remove name="trackgoober" />
        </
reference>
    </
checkout_cart_index>

    <
checkout_onepage_index translate="label">
        <
reference name="before_body_end">
            <
remove name="trackgoober" />
        </
reference>
    </
checkout_onepage_index translate="label">
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top