jQuery是否有内置函数来返回rootURL?

Tor*_*ups 8 javascript jquery

我通常使用下面的函数来返回根URL,如果我需要这个,但想想问jQuery是否有一个"单行"方式来做到这一点...

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 + "/";
            }
        }
Run Code Online (Sandbox Code Playgroud)

meo*_*ouw 12

关于什么

document.location.hostname
Run Code Online (Sandbox Code Playgroud)


Lea*_*one 9

你可以这样做:

alert(location.host)

使用location.hostname,您无法获得端口(如果有特殊端口,如:8080).