回答
尝试给浏览器一点时间来完成这项工作(我有点惊讶调整大小的出现):
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 中工作得很好,没有在其他人中测试过,但我不认为会出现问题。