我计划为同一个网站购买两个域名。根据所使用的域,我计划在页面上提供稍微不同的数据。是否有一种方法可以让我检测页面正在加载的实际域名,以便我知道要将我的内容更改为什么?
我到处找过这样的东西,但大多数都不是我想要的方式。
例如,当使用
document.write(document.location)
在JSFiddle上返回
http://fiddle.jshell.net/_display/
也就是实际的路径。
我计划为同一个网站购买两个域名。根据所使用的域,我计划在页面上提供稍微不同的数据。是否有一种方法可以让我检测页面正在加载的实际域名,以便我知道要将我的内容更改为什么?
我到处找过这样的东西,但大多数都不是我想要的方式。
例如,当使用
document.write(document.location)
在JSFiddle上返回
http://fiddle.jshell.net/_display/
也就是实际的路径。
当前回答
让我们假设你有这样的url路径:
http://localhost:4200/landing?query=1#2
因此,你可以通过位置值为自己服务,如下所示:
window.location.hash: "#2"
window.location.host: "localhost:4200"
window.location.hostname: "localhost"
window.location.href: "http://localhost:4200/landing?query=1#2"
window.location.origin: "http://localhost:4200"
window.location.pathname: "/landing"
window.location.port: "4200"
window.location.protocol: "http:"
window.location.search: "?query=1"
现在我们可以得出结论,你在寻找:
window.location.hostname
其他回答
function getDomain(url, subdomain) {
subdomain = subdomain || false;
url = url.replace(/(https?:\/\/)?(www.)?/i, '');
if (!subdomain) {
url = url.split('.');
url = url.slice(url.length - 2).join('.');
}
if (url.indexOf('/') !== -1) {
return url.split('/')[0];
}
return url;
}
例子
getDomain www.example.com’开通:/ /);/ / example.com getDomain’www.example.com’);/ / example.com getDomain(开通:/ / blog.example.com’,true);/ / blog.example.com getDomain location.href);/ /在…
以前的版本是获得全域(包括子域)。现在它根据偏好来确定正确的域。所以当第二个参数被设为真时它将包含子域,否则它只返回"主域"
结合上面的一些答案,以下是我销毁cookie的有效方法:
/**
* Utility method to obtain the domain URI:
*/
fetchDomainURI() {
if (window.location.port.length > 0) {
return window.location.hostname;
}
return `.${window.location.hostname.match(/\w*\.\w*$/gi)[0]}`;
}
适用于具有端口的IP地址,例如,0.0.0.0:8000等,以及复杂的域,如app.staging.example.com返回。example.com =>允许跨域Cookie设置和销毁。
https://publicsuffix.org/list/
(https://github.com/publicsuffix/list/blob/master/public_suffix_list.dat)
需要正确地解析出所有没有后缀的域,在上面的答案中使用点永远不会完全正确。可以对公共后缀dat文件运行上面的代码样例来实现这一点。
您可以基于此编写自己的代码,或者使用https://www.npmjs.com/package/tldts这样的包
getDomainWithoutSuffix('google.com'); // returns `google`
getDomainWithoutSuffix('fr.google.com'); // returns `google`
getDomainWithoutSuffix('fr.google.google'); // returns `google`
getDomainWithoutSuffix('foo.google.co.uk'); // returns `google`
getDomainWithoutSuffix('t.co'); // returns `t`
getDomainWithoutSuffix('fr.t.co'); // returns `t`
getDomainWithoutSuffix('https://user:password@example.co.uk:8080/some/path?and&query#hash'); // returns `example`
让我们假设你有这样的url路径:
http://localhost:4200/landing?query=1#2
因此,你可以通过位置值为自己服务,如下所示:
window.location.hash: "#2"
window.location.host: "localhost:4200"
window.location.hostname: "localhost"
window.location.href: "http://localhost:4200/landing?query=1#2"
window.location.origin: "http://localhost:4200"
window.location.pathname: "/landing"
window.location.port: "4200"
window.location.protocol: "http:"
window.location.search: "?query=1"
现在我们可以得出结论,你在寻找:
window.location.hostname
我是JavaScript的新手,但你不能只使用:文档。域?
例子:
<p id="ourdomain"></p>
<script>
var domainstring = document.domain;
document.getElementById("ourdomain").innerHTML = (domainstring);
</script>
输出:
domain.com
or
www.domain.com
这取决于你在你的网站上使用什么。