Skip to content

Instantly share code, notes, and snippets.

@hgoebl
Last active December 28, 2015 04:59
Show Gist options
  • Save hgoebl/7446989 to your computer and use it in GitHub Desktop.
Save hgoebl/7446989 to your computer and use it in GitHub Desktop.
Example code for window.onbeforeunload (not generally usable!) There are two event-handlers for loggedOut and loggedIn.
// App.logout is just a AJAX call doing logout
var App = {
// ...
logout: function () {
return $.ajax({
url: REST.postLogout,
type: 'POST'
});
}
}
App.when = {
loggedOut: function () {
$('html').removeClass('logged-in').addClass('logged-out');
window.onbeforeunload = null;
},
loggedIn: function () {
$('html').removeClass('logged-out').addClass('logged-in');
window.onbeforeunload = function () {
App.logout();
// return nothing, otherwise a confirm dialog of the browser appears
};
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment