Javascript jquery,域,获取 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2300771/
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
jquery, domain, get URL
提问by AlexC
How can I get the domain name with jquery ??
如何使用 jquery 获取域名?
回答by Sampson
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 further domain-related values, check out the properties of window.locationonline. You may find that location.hostis a better option, as its content could differ from document.domain. For instance, the url http://192.168.1.80:8080will have only the ipaddress in document.domain, but both the ipaddress and port number in location.host.
有关与域相关的更多值,请查看window.locationonline的属性。您可能会发现这location.host是一个更好的选择,因为它的内容可能与document.domain. 例如,urlhttp://192.168.1.80:8080中只有 ipaddress document.domain,但 中 ipaddress 和端口号location.host。
回答by superluminary
EDIT:
编辑:
If you don't need to support IE10, you can simply use: document.location.origin
如果你不需要支持IE10,你可以简单地使用: document.location.origin
Original answer, if you need legacy support
原始答案,如果您需要遗留支持
You can get all this and more by inspecting the location object:
您可以通过检查位置对象来获得所有这些以及更多信息:
location = {
host: "stackoverflow.com",
hostname: "stackoverflow.com",
href: "http://stackoverflow.com/questions/2300771/jquery-domain-get-url",
pathname: "/questions/2300771/jquery-domain-get-url",
port: "",
protocol: "http:"
}
so:
所以:
location.host
would be the domain, in this case stackoverflow.com. For the complete first part of the url, you can use:
将是域,在本例中为 stackoverflow.com。对于 url 的完整第一部分,您可以使用:
location.protocol + "//" + location.host
which in this case would be http://stackoverflow.com
在这种情况下将是http://stackoverflow.com
No jQuery required.
不需要jQuery。
回答by bibby
Similar to the answer before there there is
类似于之前的答案有
location.host
The location global has more fun facts about the current url as well. ( protocol, host, port, pathname, search, hash )
location global 也有更多关于当前 url 的有趣事实。(协议、主机、端口、路径名、搜索、哈希)
回答by Vova Popov
If you need from string, like me, use this function - it really works.
如果您像我一样需要字符串,请使用此功能 - 它确实有效。
function getHost(url)
{
var a = document.createElement('a');
a.href = url;
return a.hostname;
}
But note, if there is a subdomain (e.g. www.) in the URL it will get returned with the hostname. Conversely, if there is no subdomain the hostname will not have one either.
但请注意,如果 URL 中有子域(例如 www.),它将与主机名一起返回。相反,如果没有子域,主机名也不会有。
回答by Nimesh07
You can use below codes for get different parameters of Current URL
您可以使用以下代码获取当前 URL 的不同参数
alert("document.URL : "+document.URL);
alert("document.location.href : "+document.location.href);
alert("document.location.origin : "+document.location.origin);
alert("document.location.hostname : "+document.location.hostname);
alert("document.location.host : "+document.location.host);
alert("document.location.pathname : "+document.location.pathname);
回答by Adam
jQuery is not needed, use simple javascript:
不需要 jQuery,使用简单的 javascript:
document.domain
回答by jsbeckr
document.baseURIgives you the domain + port. It's used if an image tag uses a relative instead of an absolute path. Probably already solved, but it might be useful for other guys.
document.baseURI给你域+端口。如果图像标签使用相对路径而不是绝对路径,则使用它。可能已经解决了,但它可能对其他人有用。
回答by Muhammad Mohsin Muneer
var part = location.hostname.split('.');
var subdomains = part.shift();
var upperleveldomains = part.join('.');
second-level-domain, you might use
二级域,你可能会使用
var sleveldomain = parts.slice(-2).join('.');
回答by code.rider
check this
检查这个
alert(window.location.hostname);
alert(window.location.hostname);
this will return host name as www.domain.com
这将返回主机名作为 www.domain.com
回答by Zoltán Süle
as far as I see nobody posted the jquery solution but the native javascript code.
据我所知,没有人发布 jquery 解决方案,而是发布了本机 javascript 代码。
You have to pass the url as a string and it will get the host without using regexp.
您必须将 url 作为字符串传递,它会在不使用正则表达式的情况下获取主机。
function getHostFromUrl(urlString) {
return $a = $('<a>', {
'href': urlString
}).prop('host');
}

