Javascript return in window.onbeforeunload doesn’t work in chrome

google-chromejavascriptonbeforeunload

I am writing a piece of Javascript that stops a timer when it is running (active == 1) and the window is closed, a link is clicked, …

I decided to use a window.onbeforeunload function that works great on IE and FF but doesn't on Chrome.

When I close the window when having a running timer in Chrome, I do get the warning that I'm leaving the page with "false" displayed above that.

Here is the relevant code:

function SetEndTimeOnClose(lngPersonID,lngToDoID){
if(active == 1){
    var answer = confirm("Wil je de tijd stoppen?");
    if (answer){        
        try{
            StopStopwatch();
            SetEndTime(lngPersonID,lngToDoID);
        }
        catch(err){
        };
        return true;    
    }
    else{
        return false;
    }
}
}

window.onbeforeunload = function(){
    return SetEndTimeOnClose(<%=lngOpenPersonID%>,<%=lngToDoID%>);
}

Best Answer

This one works for me: http://jsfiddle.net/PQz5k/