JavaScript 窗口对象
在 JavaScript 中,假设我们有一个主页 (main.html
),其中包含 (
iframe.html
)。
现在在这个 iframe.html
中,如果我们需要引用主页上的某些内容(main.html
),我们是否可以不只指定 window
> 而不是 parent.window
。
如果答案是我们需要编写 parent.window
,我想了解是否没有一个 window
对象引用用于主页中的所有 iframe
...我确实明白 document
是特定于各个 iframe 的,但是 window
应该是所有人通用的......不是吗......请帮助我理解这个概念......
还有 window.parent
吗?如果是,它与 parent.window
有什么不同?
In JavaScript, let's say we have a main page (main.html
) which contains an <iframe>
(iframe.html
).
Now inside this iframe.html
, if we need to refer to something on the main page (main.html
), can we not just specify window
instead of parent.window
.
If the answer is we need to write parent.window
, I wanted to understand is there not a single window
object reference for all the iframes within a main page...
While I do understand document
is specific to individual iframes, but window
should be common to all..Isn't it...Please help me in understanding the concept...
Also is there something window.parent
as well? If yes, how does it differ from parent.window
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
iframe
(和frame
)是它们自己的窗口,即使在iframe
的情况下,它们看起来像是主文档的窗口。所以是的,要引用主文档的窗口,他们会使用parent
(或者window.parent
如果你想要详细但清晰),因为它们是分开的对象。这是部分必要的,因为document
中的许多内容最终都会作为包含的window
上的属性。如果您仔细考虑一下,就会发现这是有道理的:
iframe
的目的是在页面中嵌入独立来源的内容。如果主页及其上的iframe
共享一个window
对象,那么它们将共享全局上下文,并且很可能会相互冲突。免费现场示例:
父级的 HTML:
父级的 JavaScript:
子级的 HTML:
子级的 JavaScript:
iframe
s (andframe
s) are their own windows, even though in the case ofiframe
s they look like they're part of the main document's window. So yes, to refer to the main document's window, they'd useparent
(orwindow.parent
if you want to be verbose, but clear), because they are separate objects. This is partially necessary because a lot of the things indocument
end up as properties on the containingwindow
.If you think about it, it makes sense: The purpose of an
iframe
is to embed independently-sourced content within the page. If the main page and theiframe
(s) on it shared a singlewindow
object, they'd be sharing global context, and quite possibly conflicting with one another.Gratuitous live example:
Parent's HTML:
Parent's JavaScript:
Child's HTML:
Child's JavaScript:
窗口
的概念与文档
相关:每个文档
有一个窗口
,并且有一个文档
每个窗口
。这意味着
元素有自己的
document
,也有自己的window
,就像弹出窗口或主导航窗口。因此,您确实必须使用
window.parent
来访问元素的容器,就像必须使用
window.opener 一样
访问弹出窗口的所有者。编辑:
window.parent
和parent.window
都是返回相同对象的有效表达式。这是因为window
对象是脚本中的默认上下文(非限定名称被解析为window
的成员),并且window code> 对象有一个引用自身的
window
属性。因此,
parent.window
被评估为window.parent.window
,它与window.parent
是同一对象。也就是说,我确实更喜欢使用 window.parent,以避免与额外属性访问相关的(最小)开销。
The concept of
window
is tied to thedocument
: There's onewindow
perdocument
, and onedocument
perwindow
.That means
<iframe>
elements, which have their owndocument
, also have their ownwindow
, just like a pop-up window or the main navigator window.So, you'll indeed have to use
window.parent
to access the container of an<iframe>
element, just like you have to usewindow.opener
to access the owner of a pop-up window.EDIT: Both
window.parent
andparent.window
are valid expressions that return the same object. That's because thewindow
object is the default context in scripting (unqualified names are parsed as members ofwindow
), andwindow
objects have awindow
property that refers to themselves.So,
parent.window
is evaluated aswindow.parent.window
, which is the same object aswindow.parent
.That said, I do prefer using
window.parent
, to avoid the (minimal) overhead associated with the extra property access.