1

I'm trying to add a sript for sidebar only where the sidabar appear. In my local.xml I added:

<page_two_columns_left>
    <reference name="head">
        <action method="addItem"><type>skin_js</type><name>js/sidebar.js</name></action>
    </reference>
</page_two_columns_left>
<page_two_columns_right>
    <reference name="head">
        <action method="addItem"><type>skin_js</type><name>js/sidebar.js</name></action>
    </reference>
</page_two_columns_right>
<page_three_columns>
    <reference name="head">
        <action method="addItem"><type>skin_js</type><name>js/sidebar.js</name></action>
    </reference>
</page_three_columns>

but it does not work. It works in default handler

    <default>
        <reference name="head">
            <action method="addItem"><type>skin_js</type><name>js/sidebar.js</name></action>
        </reference>
    </default>

but in this case it is loaded in 1 column layout and give me a javascript error

how to add the script only where is needed?

8
  • Please add your code in page.xml file instead of local.xml – Ajay Patel Apr 15 '16 at 11:51
  • @AjayPatel I tried but it does not work either – krybbio Apr 15 '16 at 11:57
  • you need to fix only for the 2-column right? – Ajay Patel Apr 15 '16 at 11:58
  • @AjayPatel no, I need all pages but 1-column, so for the 3-colums too – krybbio Apr 15 '16 at 12:00
  • can you try ans?? – Ajay Patel Apr 15 '16 at 12:10
1

Try this Code in head.phtml file:

<?php 
   $root = Mage::app()->getLayout()->getBlock('root');
   if ($root) {
      $rootTpl = $root->getTemplate(); 
      if( $rootTpl == "page/2columns-left.phtml"){
      echo "<script src='<?php echo $this->getSkinUrl()?>default/js/sidebar.js'  </script>";     
       }  
   }  
?>
8
  • share your absolute path for js file. – Ajay Patel Apr 15 '16 at 12:15
  • \skin\frontend\MYTEMPLATE\default\js\sidebar.js – krybbio Apr 15 '16 at 12:17
  • \skin\frontend\yourtheme\default\js\sidebar.js not template – Prashant Valanda Apr 15 '16 at 12:21
  • can you tell me what is problem with updated ans? – Ajay Patel Apr 15 '16 at 12:24
  • hey. maitri.infosys this is my skype id add me so i can check on your screen – Ajay Patel Apr 15 '16 at 12:26
0

Try this instead:

<page_two_columns_left>
<block type="page/html_head" name="head" as="head">
<action method="addItem"><type>skin_js</type><name>js/sidebar.js</name></action>
</block>
</page_two_columns_left>

I don't think the head reference exists within this layout. You should also do this in your local.xml and don't edit page.xml

0
<page_two_columns_left>
    <reference name="head">
        <action method="addJs"><script>prototype/prototype.js</script></action> <!-- adds a js referencing to the /js directory -->
        <action method="addCss"><stylesheet>css/custom.css</stylesheet></action> <!-- adds CSS looking at the skin/ directories ( in reverse order: base/default, default/default, default/yourtheme, yourpackage/yourtheme -->
        <action method="addItem"><type>skin_js</type><name>js/custom_script.js</name><params/></action> <!-- adds a js at the skin/ directories in the same manner as the above addCss directive -->
    </reference>
</page_two_columns_left >

It is best to modify local.xml instead found inside app/design/frontend/yourpackage/yourtheme/layout/ if you have a custom skin to work with.

Copy is here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.