Javascript settimeout不会关闭弹出窗口

时间:2014-07-08 10:43:21

标签: javascript

大家好我正在使用此功能打开弹出窗口并在3秒后关闭它。弹出窗口打开成功,但是,settimeout函数不起作用,最终不会关闭打开的弹出窗口。 我在这里做错了什么?

<script type="text/javascript">
  function popUp(id,entry,escape)
  {
popupWindow = window.open('process_concur.php?id='+id+'&entry='+entry+'&escape='+escape,'Concur','resizable=yes,scrollbars=yes,width=250,height=250');
      popupWindow.focus();
      setTimeout(function () { popupWindow.close();}, 3000);
  }

//reload the current window when the popup is closed.
     function popUpClosed() {
         window.location.reload();
      }
  </script>

打开的弹出页面(代码)

<?php
include 'classes/class.user.php';
$userMain = new user();
//get parameters
$id = isset($_GET['id']) ? $_GET['id'] : '';
$entry = isset($_GET['entry']) ? $_GET['entry'] : '';
$escape = isset($_GET['escape']) ? $_GET['escape'] : '';
//now, $escape is sha1 and $entry is base64 encoded..decode entry and check if sha1 of decoded entry matches escape
$dentry = base64_decode($entry);
if(sha1($dentry)==$escape)
{
    //process concur
    if($userMain->reviewExists($id))
    {
        if($userMain->increaseConcur($id))
        {
            echo "Done";
            exit();
        }
    }
    else
    {
        //review doesnt no exist
        echo "Some problems occured at id doesn't exist";
    }
}
else
{
    echo "Some problems occured dentry";
}
?>

<script>
window.onunload = function() {
    if (window.opener && !window.opener.closed) {
        window.opener.popUpClosed();
    }
};
</script>

1 个答案:

答案 0 :(得分:0)

您的代码看起来是正确的。确保没有其他任何因素干扰过程。

此外,应在窗口发送关闭信号后调用popUpClosed()函数。

<script type="text/javascript">

function popUp(id,entry,escape)
{
 var popupWindow = window.open('process_concur.php?id='+id+'&entry='+entry+'&escape='+escape,'Concur','resizable=yes,scrollbars=yes,width=250,height=250');
 popupWindow.focus();
 setTimeout(function () { popupWindow.close(); popUpClosed();}, 3000);
}

//reload the current window when the popup is closed.
function popUpClosed() {
   window.location.reload();
}

popUp();
</script>