Get Magento Database URL in javascript

What is the best way to determine the Magento database url from inside javascript?

I am working on a reusable extension that needs to know the database url in javascript to make some Ajax calls. It would seem a property like

Mage.baseUrl 

will be available, but I can not find it.

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

+5
source share
1 answer

(, ) Javascript. , . -

  • local.xml

local.xml, -

<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>

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

phtml, . javascript, . ,

#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>

(, , )

+5

All Articles