Reputation: 2999
In JavaScript, what are window.opener
, window.parent
, and window.top
, and when can I use them?
Upvotes: 109
Views: 199611
Reputation: 5501
Basic information about these things can be found on MDN:
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.parent
I've used this mostly when working with <iframe>
s that need to communicate with the window object that contains them.
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.
There are a few ways you can handle your situation.
You have the following structure:
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.
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.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.appMainWindow
to reference the main window's window
object.
Upvotes: 28
Reputation: 105
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
Upvotes: 1
Reputation: 2447
top, parent, opener (as well as window, self, and iframe) are all window objects.
window.opener
-> returns the window that opens or launches the current popup window.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.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 selfUpvotes: 7
Reputation: 413737
window.opener
refers to the window that called window.open( ... )
to open the window from which it's calledwindow.parent
refers to the parent of a window in a <frame>
or <iframe>
window.top
refers to the top-most window from a window nested in one or more layers of <iframe>
sub-windowsThose 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.)
Upvotes: 182