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

时间:2022-08-23 10:33:03

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?

我怎么能做什么?

8 个解决方案

#1


71  

In a browser

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

您可以使用元素来利用浏览器的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。

#2


24  

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

#3


6  

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 Wont work for Local domains

这对本地域不起作用

When you have  URl like "https://localhost/MyProposal/MyDir/MyTestPage.aspx" 
and your virtual directory path is "https://localhost/MyProposal/" 
This case you will get this "https://localhost".

#4


6  

You can do this with plain js by using

通过使用普通的js,您可以做到这一点

  1. location.host , same as document.location.hostname
  2. 的位置。主机,与document.location.hostname相同
  3. document.domain Not recommended
  4. 文档。域不推荐

#5


4  

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 object you can get the hostname from.

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

You could either add a method to the String prototype:

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

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”

#6


4  

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

更多细节请点击这里。

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

只需在域名前面追加“http://”,以得到相应的结果。

#7


2  

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

#8


-1  

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

#1


71  

In a browser

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

您可以使用元素来利用浏览器的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。

#2


24  

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

#3


6  

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 Wont work for Local domains

这对本地域不起作用

When you have  URl like "https://localhost/MyProposal/MyDir/MyTestPage.aspx" 
and your virtual directory path is "https://localhost/MyProposal/" 
This case you will get this "https://localhost".

#4


6  

You can do this with plain js by using

通过使用普通的js,您可以做到这一点

  1. location.host , same as document.location.hostname
  2. 的位置。主机,与document.location.hostname相同
  3. document.domain Not recommended
  4. 文档。域不推荐

#5


4  

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 object you can get the hostname from.

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

You could either add a method to the String prototype:

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

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”

#6


4  

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

更多细节请点击这里。

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

只需在域名前面追加“http://”,以得到相应的结果。

#7


2  

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

#8


-1  

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