How to add external JavaScript file to Magento, so it's code would be included on every frontend page?
Note that if you'd like to add anything to the head dynamically, you can add this to your layout without the <action>, then load the block in your controller and call setText there. This method is the one that I use if I include an external js to specific magento pages. :D.
To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.
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>
Hope that helps.
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(); }
To add an external JS without any problem use this:
<reference name="head"> <block type="core/text" name="google.cdn.jquery"> <action method="setText"> <text> <![CDATA[<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script><script type="text/javascript">jQuery.noConflict();</script>]]> </text> </action> </block> </reference>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With