从iFrame获取父母文档 [英] Getting parents document from iFrame
问题描述
我已经看到了一些有关从iframe获取父母文档的优雅解决方案 - 例如这一个。但是,我无法让它工作 - 我认为这不可能是由于证券问题(跨域脚本?)?
我的问题是;我仍然可以从iframe中访问父母文档吗?或者最近这些年改变了吗?如果我能够访问这个;从iframe的内容中打印 parent.document
或 window.parent.document
时为什么会出现未定义的任何想法? 如果域匹配,仍然可以从框架内访问父级。 / p>
例如,看看这些小提琴:
- 框架主机:
fiddle.jshell.net
,父主机:fiddle.net
不匹配=失败 <测试#1: http://jsfiddle.net/nrRQg/1/ > - 框架主机:
jsfiddle.net
,父主机:jsfiddle.net
**匹配=成功*
测试#2: http://jsfiddle.net/nrRQg / 1 / show /
您可以通过以下方式访问父级: 变量 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 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: The variables 这篇关于从iFrame获取父母文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ b $如果父级是顶级文档
window.top
父母
和 top
ca n被覆盖(通常不打算)。使用 window.parent
更安全。或者,您可以用 document.defaultView
替换 window
。parent.document
or window.parent.document
from the content of the iframe?
fiddle.jshell.net
, parent host: fiddle.net
Does not match = failure
Test #1: http://jsfiddle.net/nrRQg/1/jsfiddle.net
, parent host: jsfiddle.net
**Matches = success*
Test #2: http://jsfiddle.net/nrRQg/1/show/window.parent
parent
top //If the parent is the top-level document
window.top
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
.