so i am writing some javascript to handle multiple windows and i need a way to enforce the following hierarchy: the main window should be parent to all other windows opened, regardless of where those other windows were opened from. ie. if the main window opens a child using window.open(), and the child opens a window using window.open(), these two child windows need to be siblings and children of the main window, currently one child is parent to the other and this is causing problems. is there any way to refer to the parent window before calling window.open() so the parent is set as the parent of the new window, instead of the window the javascript function is called from?
i tried this without success, but it may be close:
if (window.opener && !window.opener.closed) {
window.opener.functionThatEventuallyOpensAWindow();
} else {
functionThatEventuallyOpensAWindow();
}
if anyone has any ideas it would be greatly appreciated. please note that this code segment can not directly use window.open(), but must call other functions which eventually end in a window.open().
Found the fix. I needed to follow the function calls down to the actual window.open and add to some logic to detect if i was in a child and, if i was, call window.opener.open() instead. not sure why it was not able to change context earlier in the call stack, but it works :)
Related
Use Case: In my application i have one parent window which can open many child windows. What i have done is i have opened the child window using the window.open() method. And on the click of logout using the same reference i have closed all the windows.
But there is a catch, I am not able to retain the reference of the child window. I tried to use the localStorage of HTML5 but cause of stringify Issue with the DOM objects. I am not able to get the correct reference.
I ahve also tried saving the reference in cookies. But cookies also support the string only so i got stuck again. Please help with the same as i am trying to research on the same but no fruitful result.
I have an application containing a JavaScript view model controlling it.
The application launches a new window with (window.open()) and assigns click event listeners to buttons in the new window. New window contains a form, which when submitted, causes an unload event on its window, and thus breaking access to it from the parent window.
How can the parent's reference to this new window persist or reinstate, when the window 'unloads' and a form submits?
You can not keep that reference. However if you are trying to access some kind of values you may look into cookies or local storage.
You can also try to reinstate the reference by attaching an event listener to the "unload" event. When the event occurs close the current "parent" window, and make the "submit" script point to the parent window. Once opened you can re-open the child window from there.
Update
Cross-window javascript is a messy subject. I advise taking the first approach if it's applicable.
As far as I know, not all browsers allow to regain access to a window by name even if you provide it. But I'd still try to use distinct window names with window.open.
When trying to regain access to it - just do a window.open without url - maybe javascript:void(0) as address (so the window doesn't navigate away), it should be the same window as long as you use the same id.
it's pretty hacky though
I have a parent opening a new window, and trying to detect when the child window has loaded. In Chrome, I've tried
open('http://www.google.com').onload = function() { alert(location.href); };
but that doesn't seem to work.
Why doesn't the above method work? Are there alternatives?
If you want to be absolutely certain that a page has loaded, you can use messaging.
If you are able to use HTML5, there is a new feature called web-messaging.
This works very well on cross domain sources.
If HTML5 is not an option, use JS instead. I used a simple jQuery plugin called: windowmsg. It has worked excellently for me. I'm really not sure about this working on a cross site environment, but I think it should.
Beware of the security issues that these kind of solutions offer.
The method as it is should work(and works for me), but not when opening a window with a document from a different domain.
For security-reasons the new window will not dispatch the load-event to a document from another domain.
Technically speaking the child window and parent window are two different instances of the browser. We call them as child window and parent window. They do not have access to each other.So its not possible to perform any action in one window on the click of HTML elements in second window.
So, I suggest that you should not open a new window if you need access. You can use fancybox or lightbox that is effectively a part of the same window and that, you have control.
The work flow is like this, from my page, I created a popup using window.open method, then open other popup from that window and close its immediate parent, and I need to refresh the first parent page from this popup. I tried in different ways, but the window.opener method can't get the parent windows property.
Can anyone help me to solve this?
Normally when a window is opened using window.open I can access the caller window by using window.opener(), is it possible do the similar within modal dialogs(window.showModalDialog)?
As you can read in a comment on the MSDN page about showModalDialog (thanks to Pekka),
[t]he window.opener method returns
null, rather than a reference to the
opening window. So you cannot refresh
the opening window with
window.opener.location.refresh()
(if, for instance, you use
showModalDialog to open an editing
dialog). If all you want to do is
refresh the opening window every time
the ModalDialog closes, that is easy
(include window.location.refresh()
right after the call to
showModalDialog). But if you only
want to refresh the opening window in
certain cases (e.g., the opening
window takes a while to refresh), you
can do that by passing a
dialogArgument.
A more clever (I think) way is to
pass the window reference itself as the dialogArgument. In the
calling window, use
window.showModalDialog('newurl.asp',
window). In the called dialog
retrieve the reference with var
window_opener =
window.dialogArguments. You can use
the window reference stored in
variable window_opener in place of
window.opener, to refresh the
calling window from the called dialog.
Do note that Firefox and Chrome (for
instance) do not appear to have these
limitations, and appear to treat
ModalDialogs more like regular
windows. Keep that in mind if you do
testing using one of these browsers,
but intend your application to work in
all browsers.