从iFrame获取父母文档 [英] Getting parents document from iFrame

查看:146
本文介绍了从iFrame获取父母文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了一些有关从iframe获取父母文档的优雅解决方案 - 例如这一个。但是,我无法让它工作 - 我认为这不可能是由于证券问题(跨域脚本?)?



我的问题是;我仍然可以从iframe中访问父母文档吗?或者最近这些年改变了吗?如果我能够访问这个;从iframe的内容中打印 parent.document window.parent.document 时为什么会出现未定义的任何想法? 如果域匹配,仍然可以从框架内访问父级 / p>

例如,看看这些小提琴:



您可以通过以下方式访问父级:
$ b $如果父级是顶级文档
window.top

变量父母 top ca n被覆盖(通常不打算)。使用 window.parent 更安全。或者,您可以用 document.defaultView 替换 window


I have seen some elegant solutions on fetching the parents document from iframe - like e.g. this one. However, I can't make it work - and I didn't think this was possible due to securities issues (cross domain scripting?)?

My question is; can I still access the parents document from inside an iframe - or has this changed the recent years? If I should be able to access this; any thoughts on why I get undefined when printing parent.document or window.parent.document from the content of the iframe?

解决方案

It's still possible to access the parent from within a frame provided that the domains match.

For example, have a look at these fiddles:

You can access the parent through:

window.parent
parent
top          //If the parent is the top-level document
window.top

The variables parent and top can be overwritten (usually not intended). It's safer to use window.parent to be more safe. Alternatively, you can replace window by document.defaultView.

这篇关于从iFrame获取父母文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆