Hey All.
Have a question. I have this situation: in JavaScript I have a parent window that opens a child window using window.open(). Now, through the course of the whole thing, comes a point where the child refereshes the parent window using opener.location.reload(). After the reload happens, I have realized that the parent window no longer has references to its child windows. I need the newly reloaded parent window to still somehow have a references to the child windows so it could manipulate them.
Any ideas would be great. An example would be fantastic.
Thanks.
Hi MIGSoft,
The problem you have come across is a major one that exists in different situations in web development. It traces back to the HTTP protocol. However, thats a discussion in its own.
The reason the reference to the window does not exist any more is because the object in your parent window, that references the child window, is deleted when you refresh the parent window. So the object reference to the child window does not exist any more...
Now it would have been easy for the browser to keep the reference if it needed to, but since the web is "stateless", then any new refresh of a page is treated as a new document, unrelated to the old one.
Now one solution would be to never refresh your parent window. Instead you can use frames or iframe. Your parent frame can be used to open child windows, the child windows then refresh a frame within the parent window, so as not to refresh the parent.
The frame can not have borders, and take up 100% of the screen, to it doesnt look like you have frames.
This is the way gmail.com does it..

They also use it to keep javascript in the parent window, that they dont want refreshed.
If you are familiar with Ajax (XMLHTTPRequest), you can use a combination of frames and Ajax, or use Ajax by itself. However, for most regular sites, that would be too much work.
If you really want to refresh your parent window, then heres a few solutions. You can probably find more solutions searching "JavaScript Object Persistance". In this case, across new browser pages.
1) Using Cookies:
If you dont have too many child windows opened by the parent, you can use cookies. Cookies however have limited space to be saved in on the clients browser, so you may run out of space with lots of saving.
What you need to do it serialize (convert to string) the javascript object that you want saved for the refresh, in this case the child windows.
(search the web for Javascript serialize)
Then save the serialized object string to a cookie on the clients computer.
Then when the window is refreshed, retrieve the cookie, and unserialize. Then build the object back into the DOM.
A good example of this is :
http://developer.apple.com/internet/...nt/iframe.html
See the bit of code:
[HTML] } catch(exception) {
// This is for IE5 PC, which does not allow dynamic creation
// and manipulation of an iframe object. Instead, we'll fake
// it up by creating our own objects.
iframeHTML='\<iframe id="RSIFrame" style="';
iframeHTML+='border:0px;';
iframeHTML+='width:0px;';
iframeHTML+='height:0px;';
iframeHTML+='"><\/iframe>';
document.body.innerHTML+=iframeHTML;
IFrameObj = new Object();
IFrameObj.document = new Object();
IFrameObj.document.location = new Object();
IFrameObj.document.location.iframe = document.getElementById('RSIFrame');
IFrameObj.document.location.replace = function(location) {
this.iframe.src = location;
}
}[/HTML]
You can use this as a template to build child window objects into existance in the parent window if you cant do it with regular DOM methods. (Note: the above is building an iframe object into a parent window)
Once you have your objects in place, you're done. They should point to the child windows in existance. (I havent tested, so i may be wrong).
2) Using JSON
JavaScript Object Notation, JSON, is serializing JS Objects for transfer over HTTP. (Its become a defacto protocol for serializing JS).
YOu can use one of the JSON Libs available to serialize and un-serialize JS Objects, and even upload the Object into your server if there isnt any space on the clients computer (cookies).
With this method you will still have to build the Objects back into the DOM as in the method above.
3) Using PDOM
Persistant Object Document Model (PDOM).
This is a name I've come up with for the method I've created for making JavaScript Objects Persistant across browser pages/refreshes.
I have yet to pulish this publically but if you are interested, you can view it at
http://fijiwebdesign.com/ when it is published in a few days.
It should be the simplest method as it lets you specify which Objects you want to persist, such as your child windows, and they will be there even when the page refreshes, for as long as the browser window is open!