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
Public

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

The location for page.xml is here:
app/design/frontend/default/jm_monsieur/layout/page.xml

Insert this:

Code:

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

Before this

Code:

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

I created the jsfooter.phtml file here:
app/design/frontend/default/jm_monsieur/template/page/html/jsfooter.phtml

Place this code in the newly created file.

Code:

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

The last file I edited was 1column.phtml:
app/design/frontend/default/jm_monsieur/template/page/1column.phtml

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

Code:

<?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.

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

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:

Code:

<reference name="head">
	    <block type="core/text" name="descriptive-name">
	    <action method="setText">
	        <text><![CDATA[<link media="all" href="http://www.mywebsite.com/myfolder/styles.min.css" type="text/css" rel="stylesheet"></link><link media="all" href="http://www.mywebsite.com/myfolder/styles2.css" type="text/css" rel="stylesheet"></link>]]></text>
	    </action>
	    </block>
</reference>

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:

Code:

<reference name="before_body_end">
	    <block type="core/text" name="new-descriptive-name">
	    <action method="setText">
	        <text><![CDATA[<script type="text/javascript" src="http://www.mywebsite.com/myfolder/my-javascript.min.js"></script><script type="text/javascript" src="http://www.mywebsite.com/myfolder/my-javascript-custom.js"></script>]]></text>
	    </action>
	    </block>
</reference>

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

#3

This question is now closed

Written By

Comments