1

I am working on a single page application at the moment, and I wanting to show some UI is a user, refreshes the page, closes the tab, or navigates away, the reason for this is doing any of these will cancel any active uploads, and I want the user to know that.

So far I have,

$(window).on('beforeunload', function() {
   alert("!!!");
   return null;
});

This only seems to fire when I refresh the page, though. Is there away to hook this in to my router? I have a global var called App.Uploading, if that is true, I want to fire a method every time a route is accessed is that possible?

Udders
  • 6,914
  • 24
  • 102
  • 194
  • possible duplicate of [Backbone.js history 'on route change' event?](http://stackoverflow.com/questions/9520638/backbone-js-history-on-route-change-event) – Robert Moskal Jun 26 '15 at 15:40

1 Answers1

0

There is a JQuery unload function you can use:

$( window ).unload(function() {
    console.log('preventing unload!');
    return false;
});

document.location = 'http://www.google.com';
Richard Fernandez
  • 558
  • 1
  • 6
  • 18