Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does jQuery have a built in function to return the rootURL?

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 + "/";
            }
        }
like image 749
Toran Billups Avatar asked Jan 08 '09 15:01

Toran Billups


People also ask

How to get specific part of URL in JavaScript?

The easiest way is to use a regex or split : url = "http://localhost/solo04/index.php?Route=checkout/checkout#shipping-method"; lastPart = url. split('? ')[1]; // grabs the part on the right of the ?

How do I find the base domain?

To find the base URL of your website, go to the site's front page. What you see in the address bar on your site's front page is the base URL of your website.


3 Answers

What about

document.location.hostname
like image 191
meouw Avatar answered Nov 04 '22 06:11

meouw


You can just do:

alert(location.host)

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

like image 45
Leandro Ardissone Avatar answered Nov 04 '22 06:11

Leandro Ardissone


To get the host, you would do this:

window.location.hostname

To get the context path:

window.location.pathname

And you can redirect the user with a querystring, like this -- preserves the hostname and port

window.location.href = window.location.pathname + "?variable=" + theValue;
like image 25
101010 Avatar answered Nov 04 '22 06:11

101010