【问题标题】:How to get Domain name from URL using jquery..?如何使用 jquery 从 URL 获取域名..?
【发布时间】:2011-01-27 11:09:54
【问题描述】:

我有 eq 的域名。

1) http://www.abc.com/search 
2) http://go.abc.com/work

我只从上面的 URL 得到域名

输出类似

1) http://www.abc.com/
2) http://go.abc.com/

我该怎么办?

【问题讨论】:

  • sudhAnsu63 的回答对我有用 :)

标签: jquery


【解决方案1】:

在浏览器中

您可以使用 <a> 元素来利用浏览器的 URL 解析器:

var hostname = $('<a>').prop('href', url).prop('hostname');

或者没有 jQuery:

var a = document.createElement('a');
a.href = url;
var hostname = a.hostname;

(这个技巧对于解析相对于当前页面的路径特别有用。)

在浏览器之外(并且可能更有效):

使用以下函数:

function get_hostname(url) {
    var m = url.match(/^http:\/\/[^/]+/);
    return m ? m[0] : null;
}

像这样使用它:

get_hostname("http://example.com/path");

这将返回http://example.com/,如您的示例输出所示。

当前页面的主机名

如果您只是尝试获取当前页面的主机名,请使用document.location.hostname

【讨论】:

  • 谢谢,但我收到错误 .match not a function 会有什么问题?
  • 函数的第一个参数必须是字符串。
  • url.match(/^http.?:\/\/[^/]+/) 支持https
  • 支持Https, var m = url.match(/^https?:\/\/[^/]+/);
  • 糟糕,如果 不是有效的 url,这可能会产生意外结果。空字符串或'foobar'turn 成为当前页面的主机名。 "http://e%ample.com" 变成空字符串。验证$('&lt;a&gt;').prop('href', url).prop('href') === url 可能会有所帮助,但这也可能有奇怪的边缘情况。
【解决方案2】:

这对我有用。

http://tech-blog.maddyzone.com/javascript/get-current-url-javascript-jquery

$(location).attr('host');                        www.test.com:8082
$(location).attr('hostname');                    www.test.com
$(location).attr('port');                        8082
$(location).attr('protocol');                    http:
$(location).attr('pathname');                    index.php
$(location).attr('href');                        http://www.test.com:8082/index.php#tab2
$(location).attr('hash');                       #tab2
$(location).attr('search');                     ?foo=123

【讨论】:

  • 非常有用.. 只是一个小修正。 $(location).attr('protocol');返回带有冒号 ':' 的值,例如 http: 或 https:
  • $(location).attr('protocol');返回一个冒号(:) 即 http:
  • 比其他评论更有用
【解决方案3】:

像这样试试。

var hostname = window.location.origin

If the URL is "http://example.com/path" then you will get  "http://example.com" as the result.

这不适用于本地域

When you have URL like "https://localhost/MyProposal/MyDir/MyTestPage.aspx" 
and your virtual directory path is "https://localhost/MyProposal/" 
In such cases, you will get "https://localhost".

【讨论】:

    【解决方案4】:

    你可以通过使用普通的 js 来做到这一点

    1. location.host ,与 document.location.hostname 相同
    2. document.domain 不推荐

    【讨论】:

    • document.domain 不是检索当前主机名的正确方法;这是用于Same origin policy 的设置。另外,location.hostdocument.location.hostname ;-)
    【解决方案5】:

    你不需要 jQuery,因为简单的 javascript 就足够了:

    alert(document.domain);
    

    查看实际操作:

    console.log("Output;");  
    console.log(location.hostname);
    console.log(document.domain);
    alert(window.location.hostname)
    
    console.log("document.URL : "+document.URL);
    console.log("document.location.href : "+document.location.href);
    console.log("document.location.origin : "+document.location.origin);
    console.log("document.location.hostname : "+document.location.hostname);
    console.log("document.location.host : "+document.location.host);
    console.log("document.location.pathname : "+document.location.pathname);
    

    更多详情请点击这里window.location

    只需在域名前附加“http://”即可获得适当的结果。

    【讨论】:

      【解决方案6】:

      您可以使用一个技巧,通过创建一个&lt;a&gt;-元素,然后将字符串设置为该&lt;a&gt;-元素的href,然后您有一个Location object,您可以从中获取主机名。

      您可以向字符串原型添加一个方法:

      String.prototype.toLocation = function() {
          var a = document.createElement('a');
          a.href = this;
          return a;
      };
      

      并像这样使用它:
      "http://www.abc.com/search".toLocation().hostname

      或者让它成为一个函数:

      function toLocation(url) {
          var a = document.createElement('a');
          a.href = url;
          return a;
      };
      

      并像这样使用它:
      toLocation("http://www.abc.com/search").hostname

      这两个都会输出:"www.abc.com"

      如果你还需要协议,你可以这样做:

      var url = "http://www.abc.com/search".toLocation();
      url.protocol + "//" + url.hostname
      

      将输出:"http://www.abc.com"

      【解决方案7】:

      虽然纯 JavaScript 在这里就足够了,但我仍然更喜欢 jQuery 方法。毕竟,问题是使用 jQuery 获取主机名。

      var hostName = $(location).attr('hostname');      // www.example.com
      

      【讨论】:

        【解决方案8】:

        要获取 url 以及使用的协议,我们可以尝试下面的代码。

        例如获取域以及使用的协议 (http/https)。

        https://google.com

        你可以使用 -

        host = window.location.protocol+'//'+window.location.hostname+'/';

        它会返回协议和域名。 https://google.com/

        【讨论】:

          【解决方案9】:
          var hostname = window.location.origin
          

          不适用于 IE。 对于 IE 支持,我也会这样:

          var hostName = window.location.hostname;
          var protocol = window.locatrion.protocol;
          var finalUrl = protocol + '//' + hostname;
          

          【讨论】:

            【解决方案10】:

            试试下面的这段代码,它对我很有效。

            下面的例子是获取主机并重定向到另一个页面。

            var host = $(location).attr('host');
            window.location.replace("http://"+host+"/TEST_PROJECT/INDEXINGPAGE");
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2010-10-08
              • 1970-01-01
              • 1970-01-01
              • 2011-01-10
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多