How to Get the Base Url with Javascript for a Domain or Localhost

March 21, 2009 By

In the code below, you will be able to find the baseUrl of your website using javascript.   The following javascript code will work when used on your localhost or when it's used in a live site (finds the root url of the domain address). 

Just add this javascript function to a .js file or script tag :

function getBaseURL() {
    var url = location.href;  // entire url including querystring - also: window.location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));


    if (baseURL.indexOf('http://localhost') != -1) {
        // Base Url for localhost
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);

        return baseLocalUrl + "/";
    }
    else {
        // Root Url for domain name
        return baseURL + "/";
    }

}

If you'd like to test the getBaseURL function in an html page and view the result, simply add the following  after the closing tag of the getBaseUrl() function:

document.write(getBaseURL());

 Here's an example of the code above placed in an html page: Figure 1

 


0
18

18 Comments

anonymous by Ringo on 4/2/2009
Doug by Doug on 4/2/2009
Hey Ringo,

That localhost address should work fine with the Javascript code above. As long as the localhost address begins with: 'http://localhost' it will work.

The one issue that could occur on a live site is if you had a sub-domain that begins with the name 'localhost', for instance: 'http://localhost.mydomain.com'. That would break the code...

If you come up with other solutions, or suggestions to improve the javascript code, definitely let me know. Thanks!
anonymous by ajay kumar on 1/6/2010
thank you very much..........

awesome code. saved my time
anonymous by Pooppandi on 2/10/2010
works good. thanks for the code.
anonymous by Nehal Rupani on 2/16/2010
Hey guys,

FOR base url you can use location.host
anonymous by Adam Smith on 9/16/2010
function getUrl()
{
var pos = window.location.href.indexOf(window.location.host);
return window.location.href.substring(pos+ window.location.host.length);
}

should do the trick.
anonymous by Randy Hudson on 9/21/2010
DOES NOT WORK. The Base URL may be determined by the <base href="..."/> tag. Your code only gets the window's location, which is NOT the base URL.
Doug by Doug on 9/21/2010
Adam,

Unfortunately, your code doesn't get the root domain (or base URL), instead it gets the URL of the page and isn't a fully qualified URL for localhost.
Doug by Doug on 9/21/2010
Randy,

Your interperation of Base URL is different than mine... My code example has nothing to do with the html <base href="..." /> tag .

My code example above has to do with finding the root domain address (root URL, or base site URL, or web root) for a site using javascript, that works while in development when using a localhost project or on a production server. As a coder, I've always seen it referenced as BaseURL or BaseSiteURL... but you can call it what ever you want as far as I'm concerned.

At any rate.... YES, IT DOES WORK...
anonymous by yereverluvinunclebert on 10/22/2010
nice little snippet thanks!
anonymous by Chris Planeta on 2/2/2011
Yup. It does work very nicely.

With your consent I will include it in my naked theme I've been recently working on http://chrisplaneta.com/freebies/cornerstone-nude-opencart-theme/

Of course you will be referenced as an author. Looking forward to hearing from you
Doug by Doug on 2/2/2011
Hey Chris,

Feel free to use it in your cornerstone theme. I put the code on here, so it's definitely meant to be used by any and all!

Appreciate the reference,
- Doug
anonymous by Priyank on 4/14/2011
ThanKs,
anonymous by tony tran on 12/24/2011
that's awsome ! thanks so much
anonymous by Numerizen on 1/26/2012
Excellent script. Simple, straightforward and very usefull. Thanks a lot.
anonymous by jon on 12/14/2012
It's right!, Thanks
anonymous by mountain1 on 3/15/2013
I am being asked for a :
MYSQL CONFIG
Base URL:
Where do I find the base url?
anonymous by Anonymous on 5/2/2014
Thanks this helps.

Add your comment

by Anonymous - Already have an account? Login now!
Your Name:  

Comment:  
Enter the text you see in the image below
What do you see?
Can't read the image? View a new one.
Your comment will appear after being approved.