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
  • 101,766
  • 154
  • 423
  • 727

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,120
  • 6
  • 36
  • 61
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