views:

656

answers:

2

I typically use the below function to return the root URL if I ever need this, but thought to ask if jQuery had a "one liner" way to do this ...

function getRootURL()
        {
            var baseURL = location.href;
            var rootURL = baseURL.substring(0, baseURL.indexOf('/', 7));

            // if the root url is localhost, don't add the directory as cassani doesn't use it
            if (baseURL.indexOf('localhost') == -1)
            {
                return rootURL + "/AppName/";
            } else {
                return rootURL + "/";
            }
        }
+5  A: 

What about

document.location.hostname
meouw
He wants the context path too.
sblundy
he's adding that conditionally
meouw
right on, this is exactly what I was looking for ;)
Toran Billups
var rootURL = location.hostname + (location.hostname == 'localhost' ? '/' : '/AppName/');
CMS
+6  A: 

You can just do:

alert(location.host)

With location.hostname, you don't get the port (if there's a special port like :8080).

Leandro Ardissone