views:

312

answers:

1

So the child popup window won't close until I click the alert button, however, when I use the resize method for the child, it resizes before it shows the alert.

Here is the code on the parent page:

function ClosePopup(objWindow, strMessage) {
    objWindow.close();
    //objWindow.resizeTo(30, 30);
    if (strMessage != '') { alert(strMessage); }
    document.Block.submit();
}

And here is the code on the popup page:

$strShowMessage = "window.opener.ClosePopup(self, '$strReclassifiedLots');";

And that code gets put behind the submit event.

Any help is appreciated.

+1  A: 

Answer

Try giving the browser a moment to do the work (I'm a bit surprised the resize shows up):

function ClosePopup(objWindow, strMessage) {

    // Close the popup (or whatever)
    objWindow.close();
    //objWindow.resizeTo(30, 30);

    // Schedule our `finish` call to happen *almost* instantly;
    // this lets the browser do some UI work and then call us back
    setTimeout(finish, 0); // It won't really be 0ms, most browsers will do 10ms or so

    // Our `finish` call
    function finish() {
        if (strMessage != '') { alert(strMessage); }
        document.Block.submit();
    }
}

Note that the Block form won't be submitted until finish gets called, so if your logic is assuming that's synchronous, it may be an issue.

Demo

The parent page:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Popup Test Page</title>
<style type='text/css'>
body {
    font-family: sans-serif;
}
</style>
<script type='text/javascript'>

function ClosePopup(objWindow, strMessage) {

    // Close the popup (or whatever)
    objWindow.close();
    //objWindow.resizeTo(30, 30);

    // Schedule our `finish` call to happen *almost* instantly;
    // this lets the browser do some UI work and then call us back
    setTimeout(finish, 0); // It won't really be 0ms, most browsers will do 10ms or so

    // Our `finish` call
    function finish() {
        if (strMessage != '') { alert(strMessage); }
        document.Block.submit();
    }
}

</script>
</head>
<body><a href='popup.html' target='_blank'>Click for popup</a>
<form name='Block' action='showparams.jsp' method='POST'>
<input type='text' name='field1' value='Value in field1'>
</form>
</body>
</html>

The popup page:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Popup</title>
<style type='text/css'>
body {
    font-family: sans-serif;
}
</style>
<script type='text/javascript'>
function closeMe() {
    window.opener.ClosePopup(window, "Hi there");
}
</script>
</head>
<body>
<input type='button' value='Close' onclick="closeMe();">
</body>
</html>

I haven't included the showparams.jsp page that the form is submitted to, but all it does is dump out the fields that were submitted. The above works just fine in Chrome and IE7, didn't test it in others but I wouldn't expect a problem.

T.J. Crowder
Thanks for the answer, but it still doesn't work for some reason.I get the logic behind the setTimeout function, and why it should work, but it still doesn't.
Syncopated
@Syncopated: That's strange. It must be something else on the page, because the above *does* work, I coded up a quick test to be sure. I'm surprised it's not linked above, I'll fix that now.
T.J. Crowder
@T.J. Crowder: Yea I know it works, also created a local testfile and it worked on there so I don't have a clue ... Might resize it to 1x1 pixel, as kind of a work around :p
Syncopated