2

I'm searching for a way to let my users know that they have to log off when they are closing the window. I've already founded this code:

window.onbeforeunload = confirmExit;
function confirmExit()
{
    return "It is better if you log out, before you close this page!";
}
$(function ()
{
    $("a").click(function()
    {
        window.onbeforeunload = null;
    });
});

This works perfectly when I close my page. just as I want, but it also shows when I reload the page, which shouldn't happen.

Anyone an idea how to solve this, is it even possible?

Rings
  • 411
  • 9
  • 20
  • Not really. You're triggering `onbeforeunload` when you refresh the page. You could however bind `F5` to your own listener which refreshes the page but disables the alert(?). – Jonathan Dec 21 '14 at 21:35
  • same was here: http://stackoverflow.com/questions/2584124/javascript-register-window-tab-closing-event-before-window-tab-close but they didn't discuss the issue of page reload there. – Amr Elgarhy Dec 21 '14 at 21:37
  • You can bind on `onkeydown` event, its triggering before `onbeforeunload`. But not save from refreshing page by mouse. – sergolius Dec 21 '14 at 21:38
  • Yeah, thought of that, but indeed there's still that button in the browser wicht most people use. But isn't there a way to see if it is reloading, like checking the destination or some sort? That would totaly save my day. – Rings Dec 21 '14 at 21:46
  • I added an answer that I believe will solve most of your problems. – Larry Lane Dec 22 '14 at 13:01

1 Answers1

1

I believe this will help solve your problem. It works by first detecting if the keycode 116 has been pressed by the user(f5 key) or in other words the refresh button. If it has it will store the state of this button press so that the event beforeunload will not display the prompt. Below is the javascript/jquery you will need:

Note: This will not behave as expected in a jsfiddle so I excluded an example.

//set the variable exit to true initially because you haven't hit
//the f5 button yet to refresh
var exit = true;

//on keydown event to check if the f5 or 116 keycode has been pressed
$(window).on("keydown",function(e){//begin window on keydown event

    //if the keycode = 116
    //in other words the user has pressed the f5 key
    if(e.which === 116){//begin if then

      //set exit to false because so the prompt doesn't display
      exit = false;

    }//end if then


});//end window on keydown event


    //bind the beforeunload event to the window
    $(window).bind("beforeunload", function() {//begin event

        //if exit = true then display the prompt
        if (exit === true) {//begin if then

            //display the prompt
            //note the browser may over ride the string in your return value
            return "Are you sure you want to leave?";
        } else {

            //do nothing, no prompt will be created
            return;

        }//end if then


});//end event
Larry Lane
  • 2,141
  • 1
  • 12
  • 18
  • Works perfectly to catch the 'F5' button, but still that button on the browser, so not the full answer, but I'm afraid it isn't possible. – Rings Dec 22 '14 at 16:40
  • i have one more idea on how to do that. I will see if it works and let you know. – Larry Lane Dec 22 '14 at 18:24
  • Ok then I will test my theory and get back to you it may be tomorrow, but I will update you as soon as I can. – Larry Lane Dec 22 '14 at 22:28
  • I just wanted to let you know I haven't forgot about the solution. I am working out the bugs in my code. Right now it prevents the popup if someone hits refresh button button, but it only works every other press of the button. – Larry Lane Dec 23 '14 at 19:14
  • All my attempts have failed. As far as I know there is no way to distinguish between a user pressing the refresh button and the window close button. – Larry Lane Dec 27 '14 at 22:12
  • Thanks for trying! I'll search for an other way. – Rings Dec 29 '14 at 10:32