Wednesday, November 16, 2011

Get The Current Root URL

A very simple snippet, which stores the root URL in a variable:
[javascript]
// Retrieve root URL
var root = location.protocol + '//' + location.host;
[/javascript]

1 comments:

  1. Great and simple solution!

    Here's a solution to a related issue: Getting the base (path) of the current URL.

    You have something like:
    http://www.example.com/something/index.html

    You want:
    http://www.example.com/something/


    function getBaseUrl() {
    var re = new RegExp(/^.*\//);
    return re.exec(window.location.href);
    }


    More details here: https://404it.no/en/blog/javascript_get_base_url_or_root_url

    ReplyDelete