Javascript onbeforeunload Issue

Posted by Nik on Stack Overflow See other posts from Stack Overflow or by Nik
Published on 2010-06-17T00:25:20Z Indexed on 2010/06/17 0:32 UTC
Read the original article Hit count: 532

Alright, I have an issue with the following code. What happens is when a user closes their browser, it should prompt them to either click OK or click CANCEL to leave the page. Clicking OK would trigger a window.location to redirect to another page for user tracking (and yes, to avoid flame wars, there is a secondary system in place to assure accurate tracking, in the event of the user killing the browser from the task manager (as mentioned in similar questions)). CANCEL would remain on the page, the issue being that no matter what button you hit, you get redirected as if you wanted to leave the page. The relevant code is below.

    window.onbeforeunload = confirmExit;
function confirmExit()
{
    var where_to = confirm("Click OK to exit, Click CANCEL to stay.");
    if (where_to == true)
    {
        window.location="logout.php";
    }
    if (where_to == false){
        alert("Returning...");
    }
}

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about javascript-events