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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4815559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:58:48  来源:igfitidea点击:

How to get Domain name from URL using jquery..?

jquery

提问by Abhishek B.

I have domain name for eq.

我有 eq 的域名。

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

I get only domain name from the above URL

我只从上面的 URL 得到域名

Output like

输出像

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

how can I do?

我能怎么做?

回答by Arnaud Le Blanc

In a browser

在浏览器中

You can leverage the browser's URL parser using an <a>element:

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

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

or without jQuery:

或不使用 jQuery:

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

(This trick is particularly useful for resolving paths relative to the current page.)

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

Outside of a browser (and probably more efficiently):

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

Use the following function:

使用以下函数:

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

Use it like this:

像这样使用它:

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

This will return http://example.com/as in your example output.

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

Hostname of the current page

当前页面的主机名

If you are only trying the get the hostname of the current page, use document.location.hostname.

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

回答by Adam Mendoza

This worked for me.

这对我有用。

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

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

回答by sudhAnsu63

Try like this.

像这样尝试。

var hostname = window.location.origin

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

This won't work for local domains

这不适用于本地域

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".

回答by Clyde Lobo

You can do this with plain js by using

您可以使用普通 js 执行此操作

  1. location.host, same as document.location.hostname
  2. document.domainNot recommended
  1. location.host, 与...一样 document.location.hostname
  2. 文档域不建议

回答by Devendra Chhaiya

You don't need jQuery for this, as simple javascript will suffice:

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

alert(document.domain);

See it in action:

看看它在行动:

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);

for more details click here window.location

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

just append "http://" before domain name to get appropriate result.

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

回答by Sindre Sorhus

You can use a trick, by creating a <a>-element, then setting the string to the href of that <a>-element and then you have a Location objectyou can get the hostname from.

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

You could either add a method to the String prototype:

您可以向 String 原型添加一个方法:

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

and use it like this:
"http://www.abc.com/search".toLocation().hostname

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

or make it a function:

或者让它成为一个函数:

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

and use it like this:
toLocation("http://www.abc.com/search").hostname

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

both of these will output: "www.abc.com"

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

If you also need the protocol, you can do something like this:

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

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

which will output: "http://www.abc.com"

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

回答by Taylor Cox

While pure JavaScript is sufficient here, I still prefer the jQuery approach. After all, the ask was to get the hostname using jQuery.

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

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

回答by lazycipher

To get the url as well as the protocol used we can try the code below.

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

For example to get the domain as well as the protocol used (http/https).

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

https://google.com

https://google.com

You can use -

您可以使用 -

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

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

It'll return you the protocol as well as domain name. https://google.com/

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

回答by Prateek Kumar Jauhari

var hostname = window.location.origin

Will not work for IE. For IE support as well I would something like this:

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

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

回答by Hermes

try this code below it works fine with me.

试试下面的这段代码,它对我来说很好用。

example below is getting the host and redirecting to another page.

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

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