views:

47

answers:

2

Is there a way to popup a web page, and retrieve some information from the page as it is being closed or before being closed?

I have done me homework and know about jquery popups etc, but I want something I can learn the basics from.

/vfclists

+2  A: 
document.body.unload = PageUnload;

function PageUnload{

 // this fires when the page is unloading

}
Chris Ballance
+1  A: 

Chris Ballance's answer demonstrates how you can have JavaScript call a function when a page is closed. That's part of what you need to do. The other part is getting the information out if it.

You don't say whether you want to get that information to the server, or get it to the page that launched the popup. If you want to get it back to the server, then you can use an asynchronous AJAX call. Just be aware that could introduce delays between when the user tries to close the window and when it actually goes away. I would have the PageUnload function change some text on the page to "Closing window..." or something to let the user know that "top men are working on it".

If you're trying to push the information to the other page, then you need to use window.opener or window.parent, depending on how the popup is launched. See the w3schools documentation for more information.

dj_segfault
I am an utter javascript newbie, but from what I see the function in question exists on the page from which the popup is launched.Does that mean that all the event handling code in the popup form can be placed on the calling form, ie that even any javascript code it requires can be placed in the calling page?
vfclists
I haven't tried it, but I think the event handler for the form on the popup page would have to be on the popup page. It may be possible to reference a JavaScript function on the parent/opener page, but why add more complication?
dj_segfault