iframe 如何访问其父 DOM? [英] How is it possible for an iframe to access its parents DOM?
问题描述
中的脚本怎么可能对包含它的页面有任何概念?框架中的脚本可以访问框架之外的任何上下文吗?跨域呢?
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.
在 中,对于包含页面究竟可以做什么和不能做什么?
What exactly can and can't be done from within an <iframe>
with respect to the containing page?
推荐答案
如果 iframe 的内容与其父级具有相同的域,则可以使用 parent.document 从 iframe 访问父级页面 DOM.getElement...
.
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屋!