简体   繁体   中英

Popup window won't close until I click the alert shown on the parent page

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM