How to add script to just before </body> JM-Monsieur

How would I add a script to the bottom of the page just before </body> ?

3 answers

Profile photo of Mark Kennedy 0.00 $tone February 27, 2014

Here is a post I found on adding script to bottom of page

The location for page.xml is here:

Insert this:


<block type="page/html_head" name="jsfooter" as="jsfooter" template="page/html/jsfooter.phtml">
   <action method="addJs"><script>your_script.js</script></action>

Before this


<block type="core/text_list" name="before_body_end" as="before_body_end" translate="label">

I created the jsfooter.phtml file here:

Place this code in the newly created file.


<?php echo $this->getCssJsHtml() ?>

The last file I edited was 1column.phtml:

Add below in your template just before closing </body> tag.


<?php echo $this->getChildHtml('jsfooter') ?>

I would like to make it local to the homepage only, as it is now on every page, but that is for another day.

Profile photo of Mark Kennedy 0.00 $tone February 28, 2014

Alright so I figured out how to add external css and js for the homepage only!

It also has the advantage of being able to put the files wherever I like on the server as opposed to the default locations for addjs and addcss.

In the Magento backend Under CMS>Pages>Home page>Design you have access to page layout for only that page.

To add external css use this:


<reference name="head">
	    <block type="core/text" name="descriptive-name">
	    <action method="setText">
	        <text><![CDATA[<link media="all" href="" type="text/css" rel="stylesheet"></link><link media="all" href="" type="text/css" rel="stylesheet"></link>]]></text>

This will add two css files to the head of the document and put them below the majority of css includes.
reference name="head" references the head block in page.xml to the best of my knowledge.

To add external js use this:


<reference name="before_body_end">
	    <block type="core/text" name="new-descriptive-name">
	    <action method="setText">
	        <text><![CDATA[<script type="text/javascript" src=""></script><script type="text/javascript" src=""></script>]]></text>

This will add two external js to the bottom of the page just before </body>


This question is now closed

Written By