【发布时间】: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
我有 eq 的域名。
1) http://www.abc.com/search
2) http://go.abc.com/work
我只从上面的 URL 得到域名
输出类似
1) http://www.abc.com/
2) http://go.abc.com/
我该怎么办?
【问题讨论】:
标签: jquery
您可以使用 <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。
【讨论】:
'foobar'turn 成为当前页面的主机名。 "http://e%ample.com" 变成空字符串。验证$('<a>').prop('href', url).prop('href') === url 可能会有所帮助,但这也可能有奇怪的边缘情况。
这对我有用。
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
【讨论】:
像这样试试。
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".
【讨论】:
你可以通过使用普通的 js 来做到这一点
location.host ,与 document.location.hostname 相同
【讨论】:
document.domain 不是检索当前主机名的正确方法;这是用于Same origin policy 的设置。另外,location.host 是 document.location.hostname ;-)
你不需要 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://”即可获得适当的结果。
【讨论】:
您可以使用一个技巧,通过创建一个<a>-元素,然后将字符串设置为该<a>-元素的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"
虽然纯 JavaScript 在这里就足够了,但我仍然更喜欢 jQuery 方法。毕竟,问题是使用 jQuery 获取主机名。
var hostName = $(location).attr('hostname'); // www.example.com
【讨论】:
要获取 url 以及使用的协议,我们可以尝试下面的代码。
例如获取域以及使用的协议 (http/https)。
https://google.com
你可以使用 -
host = window.location.protocol+'//'+window.location.hostname+'/';
它会返回协议和域名。 https://google.com/
【讨论】:
var hostname = window.location.origin
不适用于 IE。 对于 IE 支持,我也会这样:
var hostName = window.location.hostname;
var protocol = window.locatrion.protocol;
var finalUrl = protocol + '//' + hostname;
【讨论】:
试试下面的这段代码,它对我很有效。
下面的例子是获取主机并重定向到另一个页面。
var host = $(location).attr('host');
window.location.replace("http://"+host+"/TEST_PROJECT/INDEXINGPAGE");
【讨论】: