【问题标题】:Popup window won't close until I click the alert shown on the parent page在我单击父页面上显示的警报之前,弹出窗口不会关闭
【发布时间】:2010-05-12 12:34:29
【问题描述】:

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

这是父页面上的代码:

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

这是弹出页面上的代码:

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

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

【问题讨论】:

    标签: javascript popup window alert


    【解决方案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 中运行良好,没有在其他人中测试过,但我不认为会出现问题。

    【讨论】:

    • 感谢您的回答,但由于某种原因它仍然无法正常工作。我得到了 setTimeout 函数背后的逻辑,以及为什么它应该工作,但它仍然没有。
    • @Syncopated:这很奇怪。它一定是页面上的其他内容,因为上面的确实有效,我编写了一个快速测试来确定。我很惊讶它上面没有链接,我现在会修复它。
    • @T.J.克劳德:是的,我知道它可以工作,还创建了一个本地测试文件并且它在那里工作,所以我不知道......可能会将其调整为 1x1 像素,作为一种解决方法:p
    猜你喜欢
    • 1970-01-01
    • 2011-05-14
    • 2017-12-06
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多