使用javascript获取包含子域名的域名

时间:2015-10-22 08:53:48

标签: javascript subdomain

我只是想知道是否有办法让某些东西成为未来的证据,因为我们可能会创建新的子域名,并希望人们知道他们使用的网站是正确的(因为我们有一些人的案例)重新托管我们的网站)

if(window.location.href != "example.com" || "*.example.com"){
document.getElementById('alert').innerHTML = "This is site is not recognized as an official site";}

哪里" *"将是任何子域的通配符,ID" alert"是一个我们用来显示重要信息的div。

2 个答案:

答案 0 :(得分:0)

使用location.hostname并检查它是否以您的域名结尾,例如像这样:

if(!location.hostname.match(/example.com$/)){
     alert(...);
}

这将匹配域 example.com 以及 www.example.com 和任何 subdomain.example.com 。但它也会匹配 otherexample.com 之类的内容。要防止这种情况,请使用以下正则表达式:

if(!location.hostname.match(/(^|\.)example.com$/)){
     alert(...);
}

此正则表达式匹配任何只有 example.com 或以点开头并以 example.com 结尾的内容,因此 www.example.com subdomain.example.com ,但不是 otherexample.com ,因为此 example.com 前面没有点。

答案 1 :(得分:0)

我宁愿使用元标题X-Frame-Options和Content-Security-Policys来解决你的重新托管问题。

X-Frame-Options:https://stackoverflow.com/a/19843216/634264

Content-Security-Policys:http://www.html5rocks.com/en/tutorials/security/content-security-policy/

相关问题