Javascript 何时使用 window.opener / window.parent / window.top

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11313045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:25:06  来源:igfitidea点击:

When to use window.opener / window.parent / window.top

javascriptwindow

提问by Sriram

In JavaScript when to use window.opener/ window.parent/ window.top?

在JavaScript时使用window.opener/ window.parent/ window.top

回答by Pointy

  • window.openerrefers to the window that called window.open( ... )to open the window from which it's called
  • window.parentrefers to the parent of a window in a <frame>or <iframe>
  • window.toprefers to the top-most window from a window nested in one or more layers of <iframe>sub-windows
  • window.opener指的是调用window.open( ... )打开调用它的窗口的窗口
  • window.parent指一个窗口的父窗口<frame><iframe>
  • window.top指嵌套在一层或多层<iframe>子窗口中的窗口中最顶层的窗口

Those will be null(or maybe undefined) when they're not relevant to the referring window's situation. ("Referring window" means the window in whose context the JavaScript code is run.)

当它们与参考窗口的情况无关时,它们将是null(或可能undefined)。(“引用窗口”是指 JavaScript 代码在其上下文中运行的窗口。)

回答by Mark At Ramp51

I think you need to add some context to your question. However, basic information about these things can be found here:

我认为您需要为您的问题添加一些上下文。但是,可以在此处找到有关这些内容的基本信息:

window.openerhttps://developer.mozilla.org/en-US/docs/Web/API/Window.opener

window.openerhttps://developer.mozilla.org/en-US/docs/Web/API/Window.opener

I've used window.opener mostly when opening a new window that acted as a dialog which required user input, and needed to pass information back to the main window. However this is restricted by origin policy, so you need to ensure both the content from the dialog and the opener window are loaded from the same origin.

我主要在打开一个新窗口时使用 window.opener,该窗口充当需要用户输入的对话框,并且需要将信息传递回主窗口。但是,这受到源策略的限制,因此您需要确保对话框和打开器窗口中的内容都是从同一源加载的。

window.parenthttps://developer.mozilla.org/en-US/docs/Web/API/Window.parent

window.parenthttps://developer.mozilla.org/en-US/docs/Web/API/Window.parent

I've used this mostly when working with IFrames that need to communicate with the window object that contains them.

我主要在处理需要与包含它们的窗口对象通信的 IFrame 时使用它。

window.tophttps://developer.mozilla.org/en-US/docs/Web/API/Window.top

window.tophttps://developer.mozilla.org/en-US/docs/Web/API/Window.top

This is useful for ensuring you are interacting with the top level browser window. You can use it for preventing another site from iframing your website, among other things.

这对于确保您与顶级浏览器窗口进行交互非常有用。您可以使用它来防止其他网站对您的网站进行 iframe,等等。

If you add some more detail to your question, I can supply other more relevant examples.

如果您在问题中添加更多细节,我可以提供其他更相关的示例。

UPDATE:There are a few ways you can handle your situation.
You have the following structure:

更新:有几种方法可以处理您的情况。
您具有以下结构:

  • Main Window
    • Dialog 1
      • Dialog 2 Opened By Dialog 1
  • 主窗口
    • 对话 1
      • 对话框 2 由对话框 1 打开

When Dialog 1 runs the code to open Dialog 2, after creating Dialog 2, have dialog 1 set a property on Dialog 2 that references the Dialog1 opener.

当对话框 1 运行代码以打开对话框 2 时,在创建对话框 2 后,让对话框 1 在对话框 2 上设置一个引用对话框 1 开启程序的属性。

So if "childwindow" is you variable for the dialog 2 window object, and "window" is the variable for the Dialog 1 window object. After opening dialog 2, but before closing dialog 1 make an assignment similar to this:

因此,如果“childwindow”是对话框 2 窗口对象的变量,而“窗口”是对话框 1 窗口对象的变量。在打开对话框 2 之后,但在关闭对话框 1 之前,进行类似于以下的分配:

childwindow.appMainWindow = window.opener

After making the assignment above, close dialog 1. Then from the code running inside dialog2, you should be able to use window.appMainWindowto reference the main window, window object.

完成上面的赋值后,关闭对话框1。然后从对话框2中运行的代码,您应该可以使用 window.appMainWindow引用主窗口,窗口对象。

Hope this helps.

希望这可以帮助。

回答by Ravish

top, parent, opener (as well as window, self, and iframe) are all window objects.

top、parent、opener(以及 window、self 和 iframe)都是窗口对象。

  1. window.opener-> returns the window that opens or launches the current popup window.
  2. window.top-> returns the topmost window, if you're using frames, this is the frameset window, if not using frames, this is the same as window or self.
  3. window.parent-> returns the parent frame of the current frame or iframe. The parent frame may be the frameset window or another frame if you have nested frames. If not using frames, parent is the same as the current window or self
  1. window.opener-> 返回打开或启动当前弹出窗口的窗口。
  2. window.top-> 返回最上面的窗口,如果你使用frames,这是frameset窗口,如果不使用frames,这和window或self一样。
  3. window.parent-> 返回当前框架或 iframe 的父框架。如果您有嵌套框架,父框架可能是框架集窗口或另一个框架。如果不使用框架,parent 与当前窗口或 self 相同

回答by Dungeon Master

when you are dealing with popups window.opener plays an important role, because we have to deal with fields of parent page as well as child page, when we have to use values on parent page we can use window.opener or we want some data on the child window or popup window at the time of loading then again we can set the values using window.opener

当你处理弹出窗口时 window.opener 起着重要的作用,因为我们必须处理父页面和子页面的字段,当我们必须使用父页面上的值时,我们可以使用 window.opener 或者我们想要一些数据在加载时在子窗口或弹出窗口上,然后我们可以再次使用 window.opener 设置值