在我单击父页面上显示的警报之前,弹出窗口不会关闭

时间:2010-05-12 12:34:29

标签: javascript popup window alert

因此,在我单击警告按钮之前,子弹出窗口不会关闭,但是,当我对子项使用resize方法时,它会在显示警报之前调整大小。

以下是父页面上的代码:

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

以下是弹出页面上的代码:

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

该代码被置于提交事件之后。

1 个答案:

答案 0 :(得分:1)

答案

尝试给浏览器一点时间来完成工作(我对调整大小显示有点惊讶):

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();
    }
}

请注意,在调用Block之前不会提交finish表单,因此如果您的逻辑假设是同步的,则可能是一个问题。

演示

父页面:

<!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>

弹出页面:

<!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>

我没有提供表单提交的showparams.jsp页面,但它所做的只是转储已提交的字段。上面的工作在Chrome和IE7中运行得很好,没有在其他人测试,但我不希望出现问题。