Categories
Magento

Add External JavaScript File in Magento

Magento e-commerce content management system has a unique combination of advanced and user-friendly features. This feature makes it one of the best content management system for designing and developing online stores. You can even add external JavaScript files in Magento to enhance the functionality of the shopping portal.

How to Include External JavaScript File in Magento?

This blog post covers the different methods you can use to include a Javascript file into Magento. Follow any one of the methods given below:

  1. The first step to include external JavaScript file in Magento is place it inside the js folder.
  2. Then include it in the XML layout with:
<reference name="head">
 <action method="addJs"><script>folder/file.js</script></action>
 </reference>

Alternate Methods to add external JavaScript file in Magento

protected function _prepareLayout()
 {   
$this->getLayout()->getBlock('head')->addJs('path/from/js/folder/to/your/file.js');
 return parent::_prepareLayout();  
}

Add External JavaScript File in Magento using Blocks

<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>

Add External JavaScript File in Magento using Methods

<action method="addJs">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</action>

Use any one of the above ways to include external JavaScript file in Magento. If you face any sort of difficulties in doing so, then request a quote. Further, our Magento team of developers will assist you in adding the file into your Magento website.