javascriptwindow.onunload

Set focus on unloading window at onunload


I have got a confirm dialog at the onunload event. It works well but there is only one problem: the window loses at onunload the focus and passes it automatically to the parent. But since there is still the confirm dialog going on I don't want to lose the focus on this window.

I have already tried to set the focus manually with window.focus() but this isn't working.

At the moment my code for the onunload event looks like this:

window.onunload = sessionConfirmation;
function sessionConfirmation(e) {
    window.focus();
    confirm('test');
}

Thank you in advance!


Solution

  • Not quite sure what your end-game is, but I'm assuming you want a confirmation dialog to show when the user tries to leave the page. If so, you want to do this instead:

    window.onbeforeunload = sessionConfirmation;
    function sessionConfirmation(e) {
    return "test";
    }
    

    Hope that's what you were asking for!