943,717 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Jun 23rd, 2006
0

Parent/Child Windows References

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Posting Pro in Training
MIGSoft is offline Offline
472 posts
since Jul 2005
Jun 28th, 2006
0

Re: Parent/Child Windows References

I think your problem is the reload, when this happens the window probably loses all relationship references (I'm guessing). If the parent refreshes, it makes sense that your code no longer controls the child. With this in mind it seems logical that the opposite is also true.

I imagine you have two options:
1. Use a frame in the child window so the child doesn't reload, only its contents (this may or may not be helpful?).
2. Use a variable or function in the child window to register the parent (this assumes that a link still exists from parent to child, even though the link is broken from child to parent).

Cheers.
Reputation Points: 20
Solved Threads: 5
Junior Poster
alpha_foobar is offline Offline
182 posts
since May 2005
Jul 3rd, 2006
0

Re: Parent/Child Windows References

Quote originally posted by MIGSoft ...
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!
Last edited by digital-ether; Jul 3rd, 2006 at 10:29 pm.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Jul 3rd, 2006
0

Re: Parent/Child Windows References

Hey Guys,

Thanks for your help. Yeah, I decided to rethink my design so it would not require the refreshing of the window. Makes things easier. Once the child finishes its thing, it refreshes the parent and then closes.

This web application is going to be running on the thin web server. It it not sophisticated enough in terms of resources, and peformance to support Ajax. Thin webserver runs a a real time OS. The html files are not even stored. What you have to do is write C-code that tells the server how to construct the html page you want on the fly.

Once again thanks for your comments.
Reputation Points: 10
Solved Threads: 1
Posting Pro in Training
MIGSoft is offline Offline
472 posts
since Jul 2005
Jul 4th, 2006
0

Re: Parent/Child Windows References

I just did an app with the iframe method suggested. It works great. The iframe window can update the database on the server and the clients parent page with only iframe refreshes via JS and it's transparent. This does file uploads, so I had to post the form.

I also did another page with a posting form that does not refresh(post), but adds a table element to the existing open page if the db update is sucessful.

Here are the links I referenced.

http://www.captain.at/howto-ajax-form-post-request.php

http://www.dustindiaz.com/add-and-re...th-javascript/

Pay close attention to the selection of the parent elements and getting the handle of those parent objects in order to update the proper objects.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
noppid is offline Offline
17 posts
since Jan 2004
Sep 6th, 2006
0

Re: Parent/Child Windows References

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.

This sounds great. Is the library available somewhere?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jpeeters is offline Offline
1 posts
since Sep 2006
Sep 19th, 2007
0

Re: Parent/Child Windows References

Hi,

I have the same problem, and I can't achieve to serialize my popup reference.

I tried with JSON and a lot of different libraries :

var popupVar = window.open('url', 'name');
var serializedString = JSON.encode(popupVar);
var serializedString = popupVar.toJSONString();
...

Every time I have errors. I could catch it with IE, and it says : [Object error]. Great :p
In Firefox it just crashs it...

I did'nt find any real example on serializing JS "native" objects, only JS "created" objects (like var Person = { this.id; this.name; })

Does someone achieve it ? Know how to serialize this popupVar ?

Thanks a lot
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mleneveut is offline Offline
3 posts
since Sep 2007
Sep 24th, 2007
0

Re: Parent/Child Windows References

Nobody ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mleneveut is offline Offline
3 posts
since Sep 2007
Sep 25th, 2007
0

Re: Parent/Child Windows References

What would you achieve by serializing a window handle which is valid only as long as the window exists?
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Sep 25th, 2007
0

Re: Parent/Child Windows References

The popup will still exist. It is just the parent window which is refreshed. So I want to store the popup reference "myRef" (var myRef = window.open(...)) before the unload of the parent window, and deserialize it when the window has finished refreshing, to have the control of the same popup (still opened).
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mleneveut is offline Offline
3 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Radio Button onClick Javascript calling only once
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: coldfusion dynamic table and Ajax





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC