Skip to content

Instantly share code, notes, and snippets.

@981746
Forked from schmuli/example.html
Created March 16, 2016 06:12
Show Gist options
  • Save 981746/3b6050052ffafef0b4df to your computer and use it in GitHub Desktop.
Save 981746/3b6050052ffafef0b4df to your computer and use it in GitHub Desktop.
A simple AngularJS service for handling the 'onbeforeunload' event
<!DOCTYPE html>
<html data-ng-app="TestApp">
<head>
<script src="http://code.angularjs.org/1.2.9/angular.js"></script>
<script>
angular.module('TestApp', [])
.factory('beforeUnload', function ($rootScope, $window) {
// Events are broadcast outside the Scope Lifecycle
$window.onbeforeunload = function (e) {
var confirmation = {};
var event = $rootScope.$broadcast('onBeforeUnload', confirmation);
if (event.defaultPrevented) {
return confirmation.message;
}
};
$window.onunload = function () {
$rootScope.$broadcast('onUnload');
};
return {};
})
.run(function (beforeUnload) {
// Must invoke the service at least once
});
function TestController($scope) {
$scope.$on('onBeforeUnload', function (e, confirmation) {
confirmation.message = "All data willl be lost.";
e.preventDefault();
});
$scope.$on('onUnload', function (e) {
console.log('leaving page'); // Use 'Preserve Log' option in Console
});
}
</script>
</head>
<body data-ng-controller="TestController">
This is a test
<a href="http://www.google.com/">Google</a>
</body>
</html>
@lprusch
Copy link

lprusch commented Mar 16, 2016

Thanks, it works like a charm!

@Github743
Copy link

Github743 commented Jun 8, 2016

If I want to do some change in database on confirming the reload by calling a service where can I do that.
On tab close in IE and fire fox it is not giving me any alert like in Chrome

@RajamohanShilpa
Copy link

Above code is not working latest version of chrome and firefox, kindly tell me any other solution for this issue.

@santaklouse
Copy link

Great solution, for me it works in latest version of FF and chromium!

@anil826
Copy link

anil826 commented Dec 1, 2016

Above code is not working for latest version of chrome I need to put custom message in prompt but I showing default message

@akashdeepshah
Copy link

@deivide
Copy link

deivide commented Mar 28, 2017

Thanks for this solution, solves my problem. I use angular 1.6.3 and it work perfectly.
@anil826, Some browsers have removed custom messages.

Copy link

ghost commented Sep 8, 2017

Thanks - this works perfectly

@maxthevoice
Copy link

👍

@vlofish
Copy link

vlofish commented May 17, 2018

Hi,

It worked perfectly except in one scenario. If I run the code and without doing any interaction with the window I click on the close button, the dialog box does not appear. I have to do some interaction in the window (like clicking the blank area) for the dialog to appear the next time I want to close the window.

Any thoughts on how to solve this issue?

@roux1max
Copy link

roux1max commented Jun 7, 2018

@MijaelWatts Maybe try to get the focus on a page element (via the .focus() method) when you load your page or emulate a click on an hidden field ?

@carlosgonzalezucv
Copy link

Excelent! thanks 🍻

@rangrasjay
Copy link

Can I rely on this for API call?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment