如何通过jquery获取域名??
发布于 2010-02-20 10:22:09
你不需要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 online的属性。您可能会发现location.host是一个更好的选择,因为它的内容可能与document.domain不同。例如,url http://192.168.1.80:8080在document.domain中只有in地址,但在location.host中既有in地址又有端口号。
发布于 2012-11-22 17:40:56
编辑:
如果不需要支持IE10,可以简单地使用:document.location.origin
如果您需要旧版support,请参阅原始答案
通过检查location对象,您可以获得所有这些信息,甚至更多:
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:"
}所以:
location.host 将是域,在本例中为stackoverflow.com。对于完整的url第一部分,您可以使用:
location.protocol + "//" + location.host不需要jQuery。
发布于 2010-02-20 16:40:11
类似于之前的答案,有
location.hostlocation global也有关于当前url的更多有趣的事实。(协议、主机、端口、路径名、搜索、散列)
https://stackoverflow.com/questions/2300771
复制相似问题