使用 javascript 获取包含子域的域名
Use javascript to get domain name including subdomains
我只是想知道是否有办法让某些东西成为未来的证明,因为我们可能会创建新的子域,并希望人们知道他们使用的网站是正确的(因为我们有几个案例重新托管我们网站的人)
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。
使用 location.hostname
并检查它是否以您的域结尾,例如像这样:
if(!location.hostname.match(/example.com$/)){
alert(...);
}
这将匹配域 example.com 以及 www.example.com 和任何 子域。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.
前面没有点
我宁愿考虑使用元 header X-Frame-Options 和 Content-Security-Policys 来解决您的重新托管问题。
X-Frame-Options: https://whosebug.com/a/19843216/634264
Content-Security-Policys: http://www.html5rocks.com/en/tutorials/security/content-security-policy/
我只是想知道是否有办法让某些东西成为未来的证明,因为我们可能会创建新的子域,并希望人们知道他们使用的网站是正确的(因为我们有几个案例重新托管我们网站的人)
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。
使用 location.hostname
并检查它是否以您的域结尾,例如像这样:
if(!location.hostname.match(/example.com$/)){
alert(...);
}
这将匹配域 example.com 以及 www.example.com 和任何 子域。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.
前面没有点我宁愿考虑使用元 header X-Frame-Options 和 Content-Security-Policys 来解决您的重新托管问题。
X-Frame-Options: https://whosebug.com/a/19843216/634264
Content-Security-Policys: http://www.html5rocks.com/en/tutorials/security/content-security-policy/