- CSS - Cascaded Style Sheets



|
|
AJAX/Javascript parent opener window close error
You have some application in a browser window. By clicking on some link,
you open a popup to do some action there. Afterwards you want to reflect
the changes done in the original browser window, by utilizing AJAX to
update the content without reloading the page.
Well, the problem here is that as soon as the popup, where you call
the AJAX update function in the window.opener window, is closed and the
AJAX request is not finished, the AJAX object is destroyed and the
callback function of the request (onreadystatechange) throws this error
in Firefox (also some error in Internet Explorer IE):
Error: [Exception... "Component returned failure code: 0x80040111
(NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]" nsresult: "0x80040111
(NS_ERROR_NOT_AVAILABLE)" location: "JS frame ::
[URL] :: [callback-onreadystatechange-function] :: line
39" data: no]
Source File: [javascript-file]
Line: 39
The solution for this is to call a function in the window.opener from the
popup, which will call the actual AJAX update function via a setTimeout.
The setTimeout will schedule the function to be executed, and regardless
if the calling popup window is still open, the function gets executed.
A sort of forking via Javascript - the function does not depend wether the
caller (the popup) is still alive (open):
POPUP:
<script>
// call settimeout function for ajax request in parent
parent.opener.loadmediadivwithtimeout();
// wait until the settimeout function in the parent-window
// has been executed, then close popup
window.setTimeout('parent.close()', 300);
</script>
OPENER:
// Don't call ajax request function directly, as it will
// be terminated when the popup is closed.
// Instead call the ajax function with settimeout:
// The settimeout function will be scheduled before
// the popup is closed and will be executed after the
// popup was closed
// Without that settimeout in the parent window, the ajax
// request will be terminated when the popup is closed
// and the ajax callback function (http_request.onreadystatechange)
// will fail (see error above)
function loadmediadivwithtimeout() {
window.setTimeout('loadmediadiv()', 30);
}
CONCLUSIONS:
If we call the javascript function loadmediadivwithtimeout() in the popup
opener window from the popup, the actual AJAX function loadmediadiv()
is scheduled to be executed in 30 milli seconds. 300 milli seconds afterwards
we close the popup, which doesn't affect the execution of the AJAX update
function.
Last-Modified: Thu, 05 Oct 2006 19:42:39 GMT
|
|