jquery 获取计算机名,如何使用jquery ..从URL获取域名?

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

我有eq的域名。

1) http://www.abc.com/search

2) http://go.abc.com/work

我只能从上述网址获得域名

输出像

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

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

我能怎么做?

Abhishek B. asked 2020-01-18T20:43:31Z

9个解决方案

82 votes

在浏览器中

您可以使用document.location.hostname元素来利用浏览器的URL解析器:

var hostname = $('').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");

在示例输出中,这将返回document.location.hostname。

当前页面的主机名

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

Arnaud Le Blanc answered 2020-01-18T20:44:30Z

36 votes

这对我有用。

[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

Adam Mendoza answered 2020-01-18T20:44:55Z

8 votes

您可以通过使用纯js来做到这一点

location.host,与document.location.hostname

document.domain不建议

Clyde Lobo answered 2020-01-18T20:45:27Z

8 votes

尝试这样。

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

sudhAnsu63 answered 2020-01-18T20:45:51Z

7 votes

您不需要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://”]即可获得适当的结果。

Devendra Chhaiya answered 2020-01-18T20:46:25Z

5 votes

您可以通过创建元素,然后将字符串设置为该"http://www.abc.com"元素的href来使用技巧,然后有一个Location对象,您可以从中获取主机名。

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

String.prototype.toLocation = function() {

var a = document.createElement('a');

a.href = this;

return a;

};

并像这样使用它:

"http://www.abc.com"

或使其功能:

function toLocation(url) {

var a = document.createElement('a');

a.href = url;

return a;

};

并像这样使用它:

"http://www.abc.com"

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

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

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

url.protocol + "//" + url.hostname

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

Sindre Sorhus answered 2020-01-18T20:47:24Z

2 votes

尽管纯JavaScript在这里已足够,但我仍然更喜欢jQuery方法。 毕竟,要问的是使用jQuery获取主机名。

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

Taylor Cox answered 2020-01-18T20:47:44Z

0 votes

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

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

https://google.com/

您可以使用 -

https://google.com/

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

lazycipher answered 2020-01-18T20:48:26Z

-1 votes

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

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

var host = $(location).attr('host');

window.location.replace("http://"+host+"/TEST_PROJECT/INDEXINGPAGE");

Hermes answered 2020-01-18T20:48:50Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值