2
<reference name="head">
<block type="fabric_tool/tool" name="js" template="fabric/tool/global.phtml"/>

<action method="addItem"><type>skin_js</type><file>js/fabric/tool/men_default.js</file></action>
</reference>

in global.phtml file, I have code like

<script>
     var BASE_URL='<?php echo Mage::getBaseUrl();?>';
</script>

in men_default.js, I have taken BASE_URL. But When application is loaded, JS is appended before global.phtml is appended. so that, I can not access BASE_URL variable in men_default.js. So What do I have to do? Is there any solution?

2

This is not possible. Because..

child blocks which are included inside head block will get only rendered after rendering/including js and css which are added in head block, irrespective of the point of inclusion in the layout update xml.

Proof : head block stands for Mage_Page_Block_Html_Head and by default it uses page/html/head.phtml file. Inside that file, you can see this

<?php echo $this->getCssJsHtml() ?>
<?php echo $this->getChildHtml() ?>

From the above code, it is obvious that, blocks included in head section will get rendered "later".

So you can probably go with below method. It may work in your case.

You need to add menu_default.js inside the phtml file itself.

That is your layout update xml should only contain the phtml file.

<reference name="head">
    <block type="fabric_tool/tool" name="js" template="fabric/tool/global.phtml"/>
</reference>

Here you need to make sure the above js block type is actually valid and does define inside magento block section.

Next update your global.phtml file with this content.

<script>
     var BASE_URL='<?php echo Mage::getBaseUrl();?>';
</script>
<script type="text/javascript" src="<?php echo $this->getSkinUrl('js/fabric/tool/men_default.js');?>"></script>

This way javascript variable BASE_URL is now defined before menu_default.js and hence you can get the value inside that file.

  • That is a good idea. But When I do as you said, It shows some errors like Invalid base url type – Vivek Aasaithambi Mar 9 '15 at 12:09
  • there was an error in the phtml code. I updated it. Use the updated code again – Rajeev K Tomy Mar 9 '15 at 12:20
  • It is ok.. I found another way to get skin url. I am using this <?php echo $this->getSkinUrl('js/fabric/tool/variables.js');?>. Anyway thank you to lead me to get solution. – Vivek Aasaithambi Mar 9 '15 at 12:29
  • ya it will also work, since it is using inside a phtml file. I will update my code with this. – Rajeev K Tomy Mar 9 '15 at 12:31
1

Try wrapping the code which is trying to access BASE_URL inside a dom:loaded function

document.observe('dom:loaded', function() {
    console.log(BASE_URL);
});

https://stackoverflow.com/questions/1907702/document-observedomloaded-function

If you're using jQuery you could use

$( document ).ready(function() {
    console.log(BASE_URL);
});

http://learn.jquery.com/using-jquery-core/document-ready/

  • Here, I cant put this.. because so many functions that are in men_default.js using BASE_URL variable. So, the functions would not wait until document ready. – Vivek Aasaithambi Mar 9 '15 at 10:21
  • Really the code that's executing should be waiting for the DOM to be fully loaded anyway. When/where are these functions called? On the initial load or view users actions? – Dave Mar 9 '15 at 11:05
  • On the initial load Some functions will be called, And user also can call those functions by clicking controls.. – Vivek Aasaithambi Mar 9 '15 at 11:06

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.