How to add internal JavaScript file to Magento, so it's code would be included on every frontend page?
Edit: i edit this question because it isn't duplicate because in other question, ask how can add external Javascript.
How to add internal JavaScript file to Magento, so it's code would be included on every frontend page?
Edit: i edit this question because it isn't duplicate because in other question, ask how can add external Javascript.
I reckon the best way is to do it by using a core/text block.
You can try adding the following to your local.xml file:
<reference name="head">
<block type="core/text" name="mycustomjavascript">
<action method="setText">
<text><![CDATA[<script src="linktoyourjavascript.js"></script>]]>
</text>
</action>
</block>
</reference>
If you don't have a local.xml file, create one under app/design/frontend/your_package/your_theme/layout.
<?xml version="1.0"?>
<layout version="0.1.0">
<default>
<reference name="head">
<block type="core/text" name="mycustomjavascript">
<action method="setText">
<text><![CDATA[<script src="linktoyourjavascript.js"></script>]]>
</text>
</action>
</block>
</reference>
</default>
</layout>
Put the JS file somewhere into the "js" folder, and in the XML layout you can include it with:
<reference name="head">
<action method="addJs"><script>folder/file.js</script></action>
</reference>
Edit: You can also do it in your block:
protected function _prepareLayout()
{
$this->getLayout()->getBlock('head')->addJs('path/from/js/folder/to/your/file.js');
return parent::_prepareLayout();
}
you can also add in page.xml
<block type="page/html_head" name="head" as="head">
<action method="addItem"><type>skin_js</type><name>path/to/file.js</name></action>
</block>
I hope this will help you.