1

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.

2
  • The title of the linked duplicate may look different, but @philwinkle's answer explains how to add JS to every page. Dec 28, 2015 at 11:17
  • isn't duplicate, search other question with this title....
    – str_lml
    Dec 28, 2015 at 11:20

2 Answers 2

0

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>
6
  • Yeah, i use these but the problem is the next: i add in local.xml these code but dont shows you have the file. Sorry for my English
    – str_lml
    Dec 28, 2015 at 10:48
  • Do you already have a local.xml or do you create one based on my solution ? Dec 28, 2015 at 10:51
  • i hadn't a local.xml, but i use ur code in my module. I have my holamundo.xml and i add the code: <action..etc. I create a local.xml in app/design/frontend/base/default/layout(this is my theme) and not found this solution
    – str_lml
    Dec 28, 2015 at 10:57
  • It's work now, but the problem is, Where do I have to place the javascript file?
    – str_lml
    Dec 28, 2015 at 10:59
  • In your post, you said it was an external javascript, not an internal one. If it's an internal javascript, please change your post. Dec 28, 2015 at 11:04
0

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.

11
  • i put my JS in C:\wamp\www\mag\js, in the console of browser(F12) say not found js
    – str_lml
    Dec 28, 2015 at 11:01
  • where you call js file? in xml? please specify tag Dec 28, 2015 at 11:03
  • I want to call internally javascript
    – str_lml
    Dec 28, 2015 at 11:06
  • you want to add code javascript in phtml? Dec 28, 2015 at 11:08
  • yes, but i have the code javascript in a file
    – str_lml
    Dec 28, 2015 at 11:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.