Include a Javascript file into Magento templates

Customising your magento store can be a good way to set your store apart from the competition so adding features that require javascript files is a common task when building a project.  Learn how to include a javscript file into your magento page templates with this quick how to.The default layout template load is made primarily through the page.xml file located at the following url for custom themed installs /app/design/frontend/your-instance-name/your-theme-name/layout/page.xml , or for the default theme at /app/design/frontend/default/default/layout/page.xml.

Find the following block encapsulator <block type=”page/html_head” name=”head” as=”head”> and simply add the following command inside the block

<action method=”addJs”><script>yourscriptname.js</script></action>

Then upload you updated page.xml file.

With that done, you need to now upload your javascript file to /skin/frontend/your-instance-name/your-theme-name/js/yourscriptname.js, or for the default theme /skin/frontend/default/default/js/yourscriptname.js

14 Comments

Jel  on November 23rd, 2009

5

yourscriptname.js

don’t you have to enter a directoryname from which the js file will be loaded?

Like:

prototype/validation.js

I would like to include a javascript file into magento, to check if magento is currently wrapped. But i cant get it to work. It seems that my generated javascript doe not do anything.

Hope to hear from you!

Greets Jel (Holland)

admin  on November 28th, 2009

6

Hi Jel,

You don’t need to mention any folder name. By default Magento will look into your JS folder of your active skin folder. Try inspecting!

Good luck

simon  on January 28th, 2010

83

hi,
Anunay Dahal
i want to add a js file only at my magento home page main content area.
do you help me to tackle with this problem.please
Thanx in advance

anunay  on February 5th, 2010

97

all you gotta do is to add a line <script type=”text/javascript” src=”your-javascript-source-path.js”></script> in a Admin->Manage Page->Edit Homepage and place it in content box and there you go. Thank you . Well if you have any please let me know.

telawat  on May 19th, 2010

210

hello anunay
i tried the code u have asked before to try ….
but the problem is it doesnt stay the same when i put this once in my homepage editor…and also could u tell me where to save the js file

zorp  on May 24th, 2010

211

To keep my custom js file in my own theme folder I found using this method much better.

skin_jsjs/custom.js

zorp  on May 24th, 2010

212

forgot a code tag

skin_jsjs/jquery-1.4.2.min.js

somay00  on July 12th, 2010

219

i want to add javascript file into catalog page. How ???

trips  on October 8th, 2010

247

Hi All,

I want to make a plugin and for that I want a page, where some analytic code is to be pasted and that code should get pasted on every page of my magento application.

It will be Javascript code.

trips  on October 8th, 2010

248

Please answer my query -
How to add javascript code to every page in magento

Magento Templates  on February 12th, 2011

264

Hello,
I want to include a javascript file into magento, to check if magento is currently wrapped or not??

krite  on October 18th, 2011

414

How did i do? before add Javascript/HTML If i want add 5 picture in head my site and in a first picture, I want add 4 picture and add Javascript/HTML What did I do.

Tutela  on November 18th, 2011

431

Hi,
How Can I do, to exernalise Javascript Code, in Magento website.

Today's Latest News  on November 24th, 2011

433

I’m using Magento 1.6, Only CSS files are added at the source when viewed in a browser, no javascripts added. Can you help?

Leave a Comment

*

Spam protection by WP Captcha-Free