iframe如何访问其父DOM? [英] How is it possible for an iframe to access its parents 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屋!