0

Hi guys I'm using this function to open a popup and close it after 3 seconds. The popup opens successfully, but, the settimeout function doesn't works and eventually doesn't close the opened popup. What am I doing wrong here?

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

The popup page which opens (Code)

<?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>
user3605847
  • 65
  • 2
  • 10
  • Wouldn't be better if you use a jQuery modal ? – Mihai Iorga Jul 08 '14 at 10:44
  • 6
    @MihaiIorga How is that related to the question? – feeela Jul 08 '14 at 10:45
  • 1
    Where is `popUpClosed()` called ? – edi9999 Jul 08 '14 at 10:45
  • "settimeout function doesn't work" What does that mean? What have you tried to debug that code? Any `console.log` are displayed? – feeela Jul 08 '14 at 10:45
  • @feeela it was a suggestion as pop-ups are, in most situations, blocked by browsers and what he wants can be achieved with a modal. – Mihai Iorga Jul 08 '14 at 10:47
  • i would use a more predictable way to close the window - something like `postMessage` – Daniel A. White Jul 08 '14 at 10:47
  • It looks correct. Do you get a console error? If you run into an uncaught exception somewhere else in the script before the timeout function runs, js *will* stop execution. – blgt Jul 08 '14 at 10:47
  • 1
    @edi9999 `popUpClosed()` is called from the popup. – user3605847 Jul 08 '14 at 10:47
  • 1
    @user3605847 you can't call one function from one window into the other – edi9999 Jul 08 '14 at 10:48
  • 4
    http://jsfiddle.net/sA4Mm/ — works for me. Whatever the problem is, it isn't revealed by the code in the question. – Quentin Jul 08 '14 at 10:48
  • @user3605847 The popup contents is of no use for the initial problem. If there is a related question, please post it as a separate question… – feeela Jul 08 '14 at 10:50
  • @edi9999 — Yes, you can. – Quentin Jul 08 '14 at 10:51
  • @Quentin How can one call a JS function from one window in another window? That would be a serious security problem… – feeela Jul 08 '14 at 10:52
  • @feeela ... it is very much possible. – Mihai Iorga Jul 08 '14 at 10:52
  • 2
    @feeela — It would only be a security problem if the windows were on different origins (which they are not, since the URL in the question is a relative one). There are security measures in place to prevent accessing most functions and other properties across origin, those measures do not apply when the origins are the same. – Quentin Jul 08 '14 at 10:52
  • 1
    In the particular case of children windows, you can execute functions of the parent window. See this : http://stackoverflow.com/questions/2550858/how-to-call-parent-window-function-in-chrome-browser – edi9999 Jul 08 '14 at 10:53
  • @Quentin Do you want me to post the full code? – user3605847 Jul 08 '14 at 11:09
  • @user3605847 — You should provide a reduced test cause that provides the minimum necessary code needed to reproduce the problem. – Quentin Jul 08 '14 at 13:01

1 Answers1

0

Your code looks correct. Ensure nothing else is interfering with the process.

Also, the popUpClosed() function should be invoked after the window is sent the close signal.

<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>
Kami
  • 19,134
  • 4
  • 51
  • 63
  • 3
    Your first paragraph is not an answer. If you can't reproduce a problem, then vote to close it, don't post answers saying that it should just work. The rest of this is commentary about best practices, not an answer to the question asked. – Quentin Jul 08 '14 at 10:54
  • Your solution made the work worse. Now the window keeps on opening and getting closed without even called. – user3605847 Jul 08 '14 at 11:00
  • @user3605847 - Please understand the code you are pasting. The last line `popUp()` will cause the popup window to appear. Remove this and call the function from your own code. – Kami Jul 08 '14 at 11:21
  • @Kami Just tried. Did not help, the popup doesn't seems to be getting closed. – user3605847 Jul 08 '14 at 11:25