如何使用jquery从URL获取域名?

Abh*_* B. 52 jquery

我有eq的域名.

1) http://www.abc.com/search 
2) http://go.abc.com/work
Run Code Online (Sandbox Code Playgroud)

我只从上面的URL获得域名

输出就好

1) http://www.abc.com/
2) http://go.abc.com/
Run Code Online (Sandbox Code Playgroud)

我能怎么做?

Arn*_*anc 83

在浏览器中

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

var hostname = $('<a>').prop('href', url).prop('hostname');
Run Code Online (Sandbox Code Playgroud)

或者没有jQuery:

var a = document.createElement('a');
a.href = url;
var hostname = a.hostname;
Run Code Online (Sandbox Code Playgroud)

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

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

使用以下功能:

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

像这样使用它:

get_hostname("http://example.com/path");
Run Code Online (Sandbox Code Playgroud)

这将http://example.com/在示例输出中返回.

当前页面的主机名

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

  • url.match(/ ^ http.?:\/\/[^/]+/)支持https (2认同)

Ada*_*oza 40

这对我有用.

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
Run Code Online (Sandbox Code Playgroud)

  • 非常有用..只需一个小修正.$(位置).attr( '协议'); 使用冒号':'返回值例如http:或https: (4认同)

Cly*_*obo 9

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

  1. location.host , 与...一样 document.location.hostname
  2. document.domain的 不建议

  • `document.domain`不是检索当前主机名的正确方法; 它是[同源政策]的设置(http://en.wikipedia.org/wiki/Same_origin_policy).另外,`location.host`是`document.location.hostname` ;-) (6认同)

sud*_*u63 9

试试这样吧.

var hostname = window.location.origin

If the URL is "http://example.com/path" then you will get  "http://example.com" as the result.
Run Code Online (Sandbox Code Playgroud)

这不适用于本地域

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".
Run Code Online (Sandbox Code Playgroud)


小智 7

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

alert(document.domain);
Run Code Online (Sandbox Code Playgroud)

看到它的实际效果:

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

有关详细信息,请单击此处window.location

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


Sin*_*hus 5

您可以通过创建<a>-element,然后将字符串设置为该<a>-element 的href 来使用技巧,然后有一个Location对象,您可以从中获取主机名。

您可以将方法添加到String原型:

String.prototype.toLocation = function() {
    var a = document.createElement('a');
    a.href = this;
    return a;
};
Run Code Online (Sandbox Code Playgroud)

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

或使其功能:

function toLocation(url) {
    var a = document.createElement('a');
    a.href = url;
    return a;
};
Run Code Online (Sandbox Code Playgroud)

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

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

如果您还需要该协议,则可以执行以下操作:

var url = "http://www.abc.com/search".toLocation();
url.protocol + "//" + url.hostname
Run Code Online (Sandbox Code Playgroud)

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