0

When a user navigates away from a page, I want to have them confirm they are leaving the page:

window.onbeforeunload = function(){
    if($('.fancy-blue-button.check-toggle').text() == 'Check In Task'){
        return 'Note that this task will automatically be checked back in once you leave this page.';
    }
};

$(window).unload(function() {
  alert('Handler for .unload() called.');
});

The first one, onbeforeunload works, the second one, unload, does not seem to do anything.

David542
  • 104,438
  • 178
  • 489
  • 842
  • This didn't help? http://stackoverflow.com/questions/2103881/capture-user-response-when-using-window-onbeforeunload/2103974#2103974 – Khawer Zeshan Jul 25 '13 at 21:44
  • Please see here: http://stackoverflow.com/a/6065085/561731 – Naftali Jul 25 '13 at 21:56
  • possible duplicate of [jQuery - beforeunload](http://stackoverflow.com/questions/6063522/jquery-beforeunload) – Naftali Jul 25 '13 at 21:56
  • 1
    I suggest you take a look at this previous post: http://stackoverflow.com/questions/1631959/browser-window-close-event/16546396#16546396 – PLT Jul 25 '13 at 22:06

2 Answers2

1

Alert will not happen in the unload function. You can try to use console.log

<script>
$(window).unload(function () {
    console.log("Bye!");
});
</script>

FIDDLE

If you want to call your script using ajax you can do it as follows

<script>
$(window).unload(function () {
    $.ajax({
        type: 'post',
        async: false,
        url: 'scriptUrlGoesHere'
    });
});
</script>
Khawer Zeshan
  • 9,470
  • 6
  • 40
  • 63
0

Maybe change your alert to a console.log and make sure your console doesn't clear on redirect ! Some browsers prevent alerts firing on unload so just make sure it is your unload not working and not the alert due to your browser

Pablo208
  • 23
  • 6