JavaScript 窗口对象

发布于 2024-11-24 01:52:12 字数 572 浏览 1 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水中月 2024-12-01 01:52:12

iframe(和 frame)是它们自己的窗口,即使在 iframe 的情况下,它们看起来像是主文档的窗口。所以是的,要引用主文档的窗口,他们会使用 parent (或者 window.parent 如果你想要详细但清晰),因为它们是分开的对象。这是部分必要的,因为 document 中的许多内容最终都会作为包含的 window 上的属性。

如果您仔细考虑一下,就会发现这是有道理的:iframe 的目的是在页面中嵌入独立来源的内容。如果主页及其上的 iframe 共享一个 window 对象,那么它们将共享全局上下文,并且很可能会相互冲突。

免费现场示例

父级的 HTML:

<p>I'm the parent window</p>
<iframe width="500" height="500" src="http://jsbin.com/iyogir"></iframe>

父级的 JavaScript:

function foo() {
  display("<code>foo</code> called!");
}
function display(msg) {
  var p = document.createElement('p');
  p.innerHTML = msg;
  document.body.appendChild(p);
}

子级的 HTML:

<p>I'm in the frame</p>
<input type='button' id='theButton' value='Click Me'>

子级的 JavaScript:

window.onload = function() {

  document.getElementById('theButton').onclick = function() {
    var p = window.parent;
    if (!p) {
      display("Can't find parent window");
    }
    else if (typeof p.foo !== "function") {
      display("Found parent window, but can't find <code>foo</code> function on it");
    }
    else {
      display("Calling parent window's <code>foo</code> function.");
      p.foo();
    }
  };

  function display(msg) {
    var p = document.createElement('p');
    p.innerHTML = msg;
    document.body.appendChild(p);
  }

};

iframes (and frames) are their own windows, even though in the case of iframes they look like they're part of the main document's window. So yes, to refer to the main document's window, they'd use parent (or window.parent if you want to be verbose, but clear), because they are separate objects. This is partially necessary because a lot of the things in document end up as properties on the containing window.

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 the iframe(s) on it shared a single window object, they'd be sharing global context, and quite possibly conflicting with one another.

Gratuitous live example:

Parent's HTML:

<p>I'm the parent window</p>
<iframe width="500" height="500" src="http://jsbin.com/iyogir"></iframe>

Parent's JavaScript:

function foo() {
  display("<code>foo</code> called!");
}
function display(msg) {
  var p = document.createElement('p');
  p.innerHTML = msg;
  document.body.appendChild(p);
}

Child's HTML:

<p>I'm in the frame</p>
<input type='button' id='theButton' value='Click Me'>

Child's JavaScript:

window.onload = function() {

  document.getElementById('theButton').onclick = function() {
    var p = window.parent;
    if (!p) {
      display("Can't find parent window");
    }
    else if (typeof p.foo !== "function") {
      display("Found parent window, but can't find <code>foo</code> function on it");
    }
    else {
      display("Calling parent window's <code>foo</code> function.");
      p.foo();
    }
  };

  function display(msg) {
    var p = document.createElement('p');
    p.innerHTML = msg;
    document.body.appendChild(p);
  }

};
救赎№ 2024-12-01 01:52:12

窗口 的概念与文档 相关:每个文档 有一个窗口,并且有一个文档 每个窗口

这意味着 元素有自己的 document,也有自己的 window,就像弹出窗口或主导航窗口。

因此,您确实必须使用 window.parent 来访问 元素的容器,就像必须使用 window.opener 一样 访问弹出窗口的所有者。

编辑: window.parentparent.window 都是返回相同对象的有效表达式。这是因为 window 对象是脚本中的默认上下文(非限定名称被解析为 window 的成员),并且 window code> 对象有一个引用自身的 window 属性。

因此,parent.window 被评估为 window.parent.window,它与 window.parent 是同一对象。

也就是说,我确实更喜欢使用 window.parent,以避免与额外属性访问相关的(最小)开销。

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

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.

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.

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文