Home > Javascript > Javascript: Get Base Url

Javascript: Get Base Url

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:

Categories: Javascript
  1. August 8, 2011 at 4:56 am

    Thank your the script….

    Accidental i need this cript….

  2. August 17, 2011 at 7:44 am

    thanks 4 the easy script ..

  3. March 28, 2012 at 1:28 pm

    works like charm,

  4. September 25, 2013 at 6:38 pm

    Thanks for the script.

  5. February 25, 2014 at 5:08 am

    Its very quick solution, good job arif.

  6. March 5, 2014 at 3:15 am

    I’m really enjoying the theme/design of your weblog.
    Do you ever run into any browser compatibility issues? A number of my blog readers have
    complained about my website not working correctly in Explorer but looks
    great in Opera. Do you have any advice to help fix this issue?

  7. March 24, 2014 at 12:44 am

    You expressed it really well.

  8. March 26, 2014 at 11:05 am

    Many thanks, Numerous data!

  9. March 28, 2014 at 3:29 am

    Very good forum posts. Thanks a lot.

  10. April 2, 2014 at 2:06 am

    This site was… howw do you say it? Relevant!! Finally I have found something that helped me.

  11. April 11, 2014 at 1:50 pm

    Useful info. Lucky me I discovered your web site accidentally,
    and I’m shocked why this twist of fate didn’t happened in advance!
    I bookmarked it.

  12. April 16, 2014 at 9:18 pm

    I am really impressed with your writing skills as well as with the layout on your weblog.

    Is this a paid theme or did you customize it yourself? Anyway keep up the excellent
    quality writing, it is rare to see a nice blog like this one nowadays.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: