0 votes
in Magento by
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

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 652 views
+1 vote
1 answer 1.2k views
0 votes
1 answer 541 views
0 votes
1 answer 8.1k views
0 votes
1 answer 1.3k views
asked Dec 16, 2016 in Magento by anonymous