如果父级本身也在 iframe 中,我如何从 iframe 中的页面判断?
解释:
我的主页 home.html
包含一个 iframe
<iframe src="sample.html"></iframe>
我需要检测 home.html
(即:sample.html
的父级)是否在 iframe 中。
sample.html
中的代码:
if(self==window)
{
alert('home.html is not in iframe');
}
else
{
alert('home.html is in iframe');
}
我的问题不是重复的。情况不同。
最佳答案
如果窗口不是框架/iframe,则为真:
if(self==top)
如果您想查看给定窗口的父窗口是否为框架,请使用:
if(parent==top)
这是top
(窗口层次结构中最顶层的窗口)和另一个窗口对象(self
或parent
)的简单比较。
关于javascript - 检查父窗口是否为 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4594492/