【问题标题】:Does jQuery have a built in function to return the rootURL?jQuery 是否具有返回 rootURL 的内置函数?
【发布时间】:2009-01-08 15:19:00
【问题描述】:

如果我需要这个,我通常使用下面的函数来返回根 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 + "/";
            }
        }

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    怎么样

    document.location.hostname
    

    【讨论】:

    • 他也想要上下文路径。
    • 他有条件地添加了
    • var rootURL = location.hostname + (location.hostname == 'localhost' ? '/' : '/AppName/');
    • 上下文路径在这里:window.location.pathname
    【解决方案2】:

    你可以这样做:

    alert(location.host)

    使用 location.hostname,您不会获得端口(如果有特殊端口,例如 :8080)。

    【讨论】:

      【解决方案3】:

      要获取主机,您可以这样做:

      window.location.hostname
      

      获取上下文路径:

      window.location.pathname
      

      您可以使用查询字符串重定向用户,如下所示——保留主机名和端口

      window.location.href = window.location.pathname + "?variable=" + theValue;
      

      【讨论】:

        【解决方案4】:

        我认为只有当 webapp 位于网站的根目录时,主机名才会给出根目录。如果它在虚拟目录或 Web 文件夹中,则结果不正确。 Refer here 获取正确答案。 我已经提到了here,它是如何轻松实现的。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-09-13
          • 2020-02-10
          • 1970-01-01
          • 2014-07-05
          • 1970-01-01
          • 2020-10-07
          • 1970-01-01
          • 2013-10-14
          相关资源
          最近更新 更多