Javascript/jQuery get root url of website

I have website: ""

Root of the site is: ""

I would like to get root url of "mywebsite" dynamicly. If I will publish this site to another place, then I will no need to change script.

For example to: ""

I will get: ""

I tried in "page.html" javascript: var url = window.location.protocol + "//" + + '/otherPage.html

but this gives me only "".

Also I know about location.origin but this is not supported for all browser as I found that on link:

If it can be done with jQuery, it will be good as well.


Here's a function doing the same as Hawk's post above, only much, much shorter:

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

Details here: Javascript: Get base URL or root URL

slightly shorter:

function getBaseUrl() {
    return window.location.href.match(/^.*\//);

if your location is fixed after mywebsite/subfolder/page.html"

then use this


Need Your Help

How should I define a JavaScript 'namespace' to satisfy JSLint?

javascript namespaces jslint

I want to be able to package my JavaScript code into a 'namespace' to prevent name clashes with other libraries. Since the declaration of a namespace should be a simple piece of code I don't want to

WCF Service and Threading

c# .net multithreading wcf

I have created a simple WCF (.NET 3.5) service which defines 10 contracts which are basically calculations on the supplied data. At the moment I expect quite few clients to make a call to some of t...