Add JavaScript to page

0 votes
13 views
asked Jun 13, 2016 in Magento by Jelena
Is there a way that we can include a JS to a Magento page without hard-coding it directly on the page?

1 Answer

0 votes
answered Jun 13, 2016 by Ivy

There are a couple of ways you can do this in Magento. There may also be any other methods with some modules or so other than what is listed below. Here a few ways that I normally use.

Method 1:
Find page/html/head.phtml in your theme and add the code directly to page.html.

Method 2:
page.xml - you can add something like

<action method="addJs"><script>path/to/my/file.js</script></action>

Method 3:
If you check the stock page.html mentioned above, you can see this line

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

In normal cases, the getChildHtml method is used to render a specific child block. But, if this is called with no other paramaters, then getChildHtml will automatically render all the child blocks available. Which also means you can add something like

<!-- existing line -->
<block type="page/html_head" name="head" as="head">
<!-- new sub-block you're adding -->
<block type="core/template" name="mynewtemplate" as="mynewtemplate" template="page/mynewtemplate.phtml"/>...

to page.xml, and then add the mynewtemplate.phtml file.
Any block added to the head block will be automatically rendered. (this automatic rendering process does not apply for all layout blocks, only for blocks where getChildHtml is called without paramaters) as mentioned above.

Related questions

+1 vote
0 answers 257 views
+1 vote
1 answer 600 views
0 votes
1 answer 372 views
0 votes
1 answer 409 views
asked Dec 16, 2016 in Magento by anonymous
0 votes
3 answers 399 views
0 votes
1 answer 365 views
...