iframe如何访问其父DOM? [英] How is it possible for an iframe to access its parents DOM?

查看:187
本文介绍了iframe如何访问其父DOM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<iframe>中的脚本如何对包含该脚本的页面有任何概念?框架中的脚本可以访问其外部的任何上下文吗?跨域呢?

How is it possible for a script within an <iframe> to have any notion of the page containing it? Can a script in the frame access any context outside of it? What about cross-domain?

到目前为止,我认为<iframe>对于包含页面完全是不可知的,但是我看到了一个与这种观点相矛盾的示例.

Up until now I believed an <iframe> is completely agnostic to the containing page, but I have seen an example which contradicts this belief.

<iframe>中相对于包含页面到底可以做什么和不能做什么?

What exactly can and can't be done from within an <iframe> with respect to the containing page?

推荐答案

如果iframe的内容与其父级具有相同的域,则可以使用parent.document.getElement...从iframe访问父级页面DOM.

If the content of the iframe and its parent have the same domain, you can access the parent pages DOM from the iframe by using parent.document.getElement....

但是,您无法执行此跨域操作(即使跨不同的子域也是如此),因为它会导致:

However you can't do this cross-domain (not even across different subdomains) as it will result in:

Uncaught DOMException: Blocked a frame with origin "https://example.com" from accessing a cross-origin frame.

这篇关于iframe如何访问其父DOM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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