Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding external JavaScript file to Magento

How to add external JavaScript file to Magento, so it's code would be included on every frontend page?

like image 358
Magento Kid Avatar asked Jan 11 '11 06:01

Magento Kid


People also ask

How can you add an external JavaScript CSS file to Magento?

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.

How do I connect an external JavaScript file?

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.


2 Answers

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();     } 
like image 43
OSdave Avatar answered Sep 29 '22 11:09

OSdave


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> 
like image 127
Gaurav Tewari Avatar answered Sep 29 '22 11:09

Gaurav Tewari