0

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

这是父页面上的代码:

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

这是弹出页面上的代码:

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

并且该代码被放在提交事件后面。

4

1 回答 1

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 中工作得很好,没有在其他人中测试过,但我不认为会出现问题。

于 2010-05-12T12:38:11.457 回答