5

What is the best way to determine a Magento store's base url from inside javascript?

I'm working on a reusable extension that needs to know the store's base url in javascript in order to do some Ajax calls. One would think a property like

Mage.baseUrl 

would be available, but I can't find it.

An alternative would be to add the base url as a bit of inline javascript, but I can't find any information on how to add inline javascript programmatically (only external js files), without altering the template.

1 Answer 1

5

By default this information isn't (reliably, stably) exposed via Javascript. You're going to need to expose it yourself via a custom block added to the layout. The easiest way to do this will be

  1. Adding the block via your theme's local.xml file

  2. Adding a template to your theme for the above block

To add the block to the layout via your local.xml file, something like this should suffice

<default>
    <reference name="root">
        <block name="my_custom_js_block">
            <action method="setTemplate">
                <template>my_custom_js_block/extra-js.phtml</template>
            </action>
        </block>
    </reference>
</default>

Then add the following folder and file to your theme

app/design/frontend/default/your_theme/template/my_custom_js_block/
app/design/frontend/default/your_theme/template/my_custom_js_block/extra-js.phtml

At this point you have a phtml template file that will be rendered on every page. You can add whatever javascript variables you want. I'm fond of a pattern something like

#File: app/design/frontend/default/your_theme/template/my_custom_js_block/extra-js.phtml
<?php
    $h = Mage::helper('core');
    $info = new stdClass();
    $info->base_dir = Mage::getBaseDir();
?>
<script type="text/javascript">
    var my_custom_js_block_info = <?php echo $h->jsonEncode($info); ?>;
</script>

(untested, top-of-my-head code, but should work)

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the detailed answer. How could I add this block from my module's config.xml file, rather than local.xml? I'm making a reusable module and can't rely on the user modifying local.xml.
Google around for adding a custom layout XML file in your module's config.xml. Just like Mage_Catalog adds catalog.xml, you can have your module include a custom XML file.
After a few hours of searching and trying the many contradicting and different methods to accomplish this, I have failed. Is there a way to know if my custom xml file is even being included at run time?
Make sure you're in developer mode and then introduce and error into the XML file. If PHP chokes on including the file, you know it's being loaded. See also: magento-quickies.tumblr.com/post/16336482158/…
Alright got it finally. The key is that your custom layout file has to be under /app/design/frontend/[package]/[theme]/layout, NOT your module's etc directory. So I put mine at /app/design/frontend/base/default/layout/mymodule.xml and got it working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.