Javascript窗口对象 [英] Javascript window object

查看:100
本文介绍了Javascript窗口对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Javascript中,假设我们有一个主页( main.html ),其中包含一个< iframe> iframe.html

In Javascript, let's say we have a main page (main.html) which contains an <iframe> (iframe.html)

现在里面这个 iframe.html ,如果我们需要引用主页上的东西( main.html
,我们不能只指定窗口而不是 parent.window

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

如果答案是我们需要写 parent.window ,我想明白主页中所有的iframe都没有一个窗口对象引用。

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..

虽然我明白文档特定于单个iframe,但窗口应该是所有的共同的...不是...请帮助我理解概念...

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...

还有东西 window.parent 以及?如果是,它与 parent.window 有什么不同?

Also is there something window.parent as well ? If yes, how does it differ from parent.window ?

推荐答案

窗口的概念与文档相关:有一个窗口每个文档和一个文档窗口

The concept of window is tied to the document: There's one window per document, and one document per window.

这意味着< iframe> 元素,它们有自己的文档,也有自己的窗口,就像一个弹出窗口或主导航窗口。

That means <iframe> elements, which have their own document, also have their own window, just like a pop-up window or the main navigator window.

所以,你必须使用 window.parent 访问< iframe> 元素,就像您必须使用 window.opener 访问弹出窗口的所有者。

So, you'll indeed have to use window.parent to access the container of an <iframe> element, just like you have to use window.opener to access the owner of a pop-up window.

编辑: window.parent parent.window 都是返回相同的有效表达式目的。这是因为窗口对象是脚本中的默认上下文(不合格的名称被解析为窗口的成员)和窗口对象具有引用自己的窗口属性。

Both window.parent and parent.window are valid expressions that return the same object. That's because the window object is the default context in scripting (unqualified names are parsed as members of window), and window objects have a window property that refers to themselves.

所以, parent.window 被评估为 window.parent.window ,这是与 window.parent 相同的对象。

So, parent.window is evaluated as window.parent.window, which is the same object as window.parent.

表示,我更喜欢使用窗口.parent ,以避免与附加属性访问相关联的(最小)开销。

That said, I do prefer using window.parent, to avoid the (minimal) overhead associated with the extra property access.

这篇关于Javascript窗口对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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