LoB*_*LoB 1 javascript if-statement boolean conditional-statements
我有一些代码来保护我的页面不被iframed.
window.onload = function(){
try
{
if (window.parent && window.parent.location.hostname !== "app.herokuapp.com"){
throw new Error();
}
catch (e){
//do something
}
}
Run Code Online (Sandbox Code Playgroud)
它完全正常,直到我尝试添加更多值来比较主机名.我想添加自定义域名.我试过这个:
window.onload = function(){
try
{
if (window.parent && (window.parent.location.hostname !=="app.herokuapp.com"
|| window.parent.location.hostname !== "www.app.com"
|| window.parent.location.hostname !== "localhost")){
throw new Error();
}
catch (e){
//do something
}
}
Run Code Online (Sandbox Code Playgroud)
这总是返回true,因此会抛出错误.我怎样才能做到这一点?除非主机名匹配这些字符串,否则我想抛出一个错误,无论如何都会抛出错误.我是新来的,会喜欢一些帮助!谢谢.
PS.我添加了"localhost",因为我希望能够在推送到heroku之前在本地测试它.
||
true
如果有任何操作数求值,则返回计算结果true
.也许你的意思是使用&&
:
if (window.parent
&& window.parent.location.hostname !== "app.herokuapp.com"
&& window.parent.location.hostname !== "www.app.com"
&& window.parent.location.hostname !== "localhost")
Run Code Online (Sandbox Code Playgroud)
或者根据德摩根定律:
if (window.parent
&& !(window.parent.location.hostname === "app.herokuapp.com"
|| window.parent.location.hostname === "www.app.com"
|| window.parent.location.hostname === "localhost"))
Run Code Online (Sandbox Code Playgroud)
这将评估true
是否所有操作数都评估为true
.
进一步阅读