[英]Javascript Function like Objects i.e. “$” can be used as a function e.g. $() as well as an object $
[英]JavaScript to test for an object and child and grandchild objects (e.g.: HTML Frames)
我有一部分JavaScript,仅当某个框架集可用时,我才希望启用它。
我想做的就是:
if (typeof(top.TableOfContents.frames.content)!=='undefined') {
// stuff I want to do only if there's a frame named
// TableOfContents with a frame named content
}
现在,如果框架不可用,我会收到错误消息,所以我现在要做的是:
if (typeof(top.TableOfContents)!=='undefined'
&& typeof(top.TableOfContents.frames)!=='undefined'
&& typeof(top.TableOfContents.frames.content)!=='undefined'){
// stuff I want to do only if there's a frame named
// TableOfContents with a frame named content
}
但这似乎有更好的方法(除了丢帧之外,哈哈)……还有没有更有效或更简单的方法来测试对象,子对象和孙对象?
现在,这些都是使用相对URL引用的框架,因此应该没有我能想到的“ 同一来源策略 ”问题(我的测试中没有遇到任何问题)。
我喜欢在这种情况下使用筛子
if (!!((((top || {}).TableOfContents || {}).frames || {}).content)) {
// stuff you want to do only if there's a frame named
// TableOfContents with a frame named content
}
double not(!!)进行显式的布尔类型转换。
这是您已经在做的控制(仅此而已),但效率更高
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.