javascript - 检查父窗口是否为 iframe

标签 javascript jquery iframe

如果父级本身也在 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(窗口层次结构中最顶层的窗口)和另一个窗口对象(selfparent)的简单比较。

关于javascript - 检查父窗口是否为 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4594492/

相关文章:

javascript - 在 MVC 中设置多选下拉列表的选定值

javascript - jQuery - Google 图像功能在点击时滚动到无序列表行的顶部

Facebook 评论插件 - 如何在 iframe 后面 Conceal 评论?

django - 了解 Django CSRF_COOKIE_SAMESITE 和 CSRF_TRUSTED_ORIGINS

javascript - 为什么 Node.js 中不推荐使用域 API?

javascript - 使用IEWebGL时如何检测WebGL是否可用?

javascript - 如何在 JavaScript 中获取 Java boolean 值?

javascript - css transitions 影响之前修改过的 css 属性

基于Class的jQuery自定义验证

apache - 为网站上的所有 url 添加 X-Frame-Options(启用列表除外)