Adding and removing javascript / css when and where you need it
Posted on 19. Feb, 2009 by Fido in CMS, Design, Development, Magento
Adding and removing javascript and css is handled separately within Magento. CSS is added in the usual fashion, where you have a <link rel=”stylesheet”… />. However, any included javascript (unless linked to “by hand” from a theme’s skin) is pulled via a php files which reads through the “js” folder in the root directory (root/js/index.php is responsible for this).
That is all well and good for Magento. The real question is how we, as developers, add these items when we need them. How you as a developer add css or javascript is, luckily, handled the same.
In this post, we will show how to add and remove javascript and css to a CMS page (and anywhere else) that you may need.
Method 1: Layout xml.
a) For files you want to include on every page
For css or js files you want to add to every page, you edit the page.xml files located in your layout folder (app/design/frontend/default/your_theme/layout/page.xml). Within this file, at the top you will find the <default> area with the <block name=”root” … >. This block has a child named “head” which contains the included css and js elements.
<block type="page/html_head" name="head" as="head"> <action method="addJs"><script>prototype/prototype.js</script></action> <action method="addJs" ifconfig="dev/js/deprecation"><script>prototype/deprecation.js</script></action> <action method="addJs"><script>prototype/validation.js</script></action> <action method="addJs"><script>scriptaculous/builder.js</script></action> ... </block>
Here you can add your javascript and css. Note that any Js files you add are relative to the “js” folder in your root directory. The css files are included from the skin files of the current and default templates (skin/frontend/default/your_template(& default)/css).
b) Specific areas
If you want to include css or js on only certain areas (such as the checkout process or catalog pages), you can do that also. For instance, if you want it in a single product view (product view vs product list), you can open up catalog.xml, find <catalog_product_view> area (near line 168 in vs 1.2.1). Add your code in there – notice that we are using the <reference> tag rather than <block> tags. We use the “reference” tag to reference other blocks that are in other areas of the template.
<reference name="head"> <action method="addJs"><script>varien/product.js</script></action> <action method="addItem"><type>js_css</type><name>calendar/calendar-win2k-1.css</name><params/><!--<if/><condition>can_load_calendar_js</condition>--></action> <action method="addItem"><type>js</type><name>calendar/calendar.js</name><!--<params/><if/><condition>can_load_calendar_js</condition>--></action> <action method="addItem"><type>js</type><name>calendar/lang/calendar-en.js</name><!--<params/><if/><condition>can_load_calendar_js</condition>--></action> <action method="addItem"><type>js</type><name>calendar/calendar-setup.js</name><!--<params/><if/><condition>can_load_calendar_js</condition>--></action> </reference>
The use of
Method 2: Block Code
We can accomplish all of this in code as well. These functions are defined within Mage_Page_Block_Html_Head. So, we can use this code with in a block class (not a .phtml file!):
$headBlock = $this->getLayout()->getBlock('head');
$headBlock->addJs('somefolder/yay.js');
I suggest looking over the page.xml files as long as finding the removeItem syntax ($type, $name for the method, for the xml), which will be very handy for you for adding or removing assets as and when you need them!
<action method="removeItem"><type>js</type><name>calendar/calendar.js</name</action>
$this->getLayout->getBlock('head')->removeItem('js', 'calendar/calendar.js');




15 Comments
anton78
28. Feb, 2009
Good article. Thanks for sharing. I was wondering… I have separate css stylesheets that coordinate with different .phtml template layout files, 1 column, 2 column, 3 column. How would you load different stylesheets according to the page currently being viewed? Is this even recommended or do you have a better suggestion?
Charlie
03. Mar, 2009
Hello,
I just wanted to take a min. to say thanks for taking the time to publish help topics for Magento. I have been banging my head against the wall for a while trying to put my hands around the flow of Magento. They have so many files depending on other files and pointing all over the place its like a web of confusion. Thanks,
Charlie
matt
22. Mar, 2009
Nice introduction and explanation. In the interest of separating core code from design and content, perhaps it’s best to guide developers away from putting front-end level code in block files
Fido
24. Mar, 2009
Matt – I agree, with the stipulation that it depends on your implementation of the code. If you are creating an entire module, you should add this via a layout xml file.
None of this should go into a core file, block variety or otherwise.
shahriat
26. Mar, 2009
g8 article, its better add or remove any js or css files by modifying the layout xml files for the next generation
Magento Developer
29. Apr, 2009
Hi there,
Really good article ..Also now I will read your blog & i will read your all post I am really happy after getting this blog.
Thanks,
Samir
Web Design Lancashire
09. May, 2009
I’m about to start developing a magento ecommerce based website, i’ll keep this information in mind.
Thanks
Igor
18. Jun, 2009
I don’t understand how to remove css for page.
For example for print order page I don’t need main.css file.
main.css file located at section in file page.xml
In case I wrote in file sales.xml section such
csscss/main.css</name
or
csscss/main.css</name
then I’ll get errors.
Could you please give me advice how to remove css ?
Magento Themes
12. Sep, 2009
very helpful. thanks for the post. xml newbie here lol
amtec
18. Sep, 2009
How do you add remote files ?
Like http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.3/prototype.js ?
vickeey
09. Oct, 2009
I place the .js file in root/js/stepcarousel.js
wat path i should mention?
or
Stinky59
22. Oct, 2009
Click Add, and then click New. ,
newberemis
24. Nov, 2009
Thanks you
sandra83smith
18. Dec, 2009
Thanks, nice article and provide useful information.
Hazel
21. Dec, 2009
Many thanks for sharing this. It helped me out…
Leave a reply