Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7076433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:17:52+00:00 2026-05-28T06:17:52+00:00

What is the best way to determine a Magento store’s base url from inside

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:17:52+00:00Added an answer on May 28, 2026 at 6:17 am

    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is this the best way to determine that the List<int> ALogMsgTypeIntArray contains a 0?
what's the best way to determine whether a Javascript variable is an array, but
A question that occasionally arises is what is the best way to determine the
What is the best way to determine the number of elements in an array
What is the best way to determine whether or not a field's value is
Please let me know what is the best way to determine composition of young
given any number, what's the best way to determine it is even? how many
What is the best way to programatically determine if a Perl script is executing
I am trying to determine the best way to create a treeview list in
What is the best way to do a boolean test in C# to determine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.