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

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

问题描述

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

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?)?

我的问题是;我仍然可以从 iframe 内部访问父文档 - 或者近年来这是否发生了变化?如果我应该能够访问它;关于为什么在从 iframe 的内容打印 parent.documentwindow.parent.document 时我得到未定义的任何想法?

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:

  • Frame host: fiddle.jshell.net, parent host: fiddle.net Does not match = failure
    Test #1: http://jsfiddle.net/nrRQg/1/
  • Frame host: jsfiddle.net, parent host: jsfiddle.net **Matches = success*
    Test #2: http://jsfiddle.net/nrRQg/1/show/

您可以通过以下方式访问父级:

You can access the parent through:

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

变量 parenttop 可以被覆盖(通常不是故意的).使用 window.parent 更安全更安全.或者,您可以将 window 替换为 document.defaultView.

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天全站免登陆