1

I have to store closingtime after closing of tab and browser.that closing time should be stored in localstorage in angularjs.

I am using $localStorage and $window directive.

Gaurav Kumar
  • 241
  • 1
  • 4
  • 12

3 Answers3

1

You can use... beforeunload

$(document).ready(function()
{
    $(window).bind("beforeunload", function() { 
        return "Do you really want to close?"; 
    });
});

With pure JS

window.addEventListener("beforeunload", function (e) {

});

https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload?redirectlocale=en-US&redirectslug=DOM%2FMozilla_event_reference%2Fbeforeunload

Thalaivar
  • 23,282
  • 5
  • 60
  • 71
  • yes your code is alerting me for leav browser or stay on browser in working on closing of tab or browser but – Gaurav Kumar Oct 22 '16 at 13:00
  • your code is alerting me for closing or staying into browser but how can i store closing time on close of browser|| tab .Please suggest? – Gaurav Kumar Oct 22 '16 at 13:04
  • you can use $locationChangeStart or $stateChangeSuccess with the beforeunload event... look at this directive... http://stackoverflow.com/a/22866537/337128 – Thalaivar Oct 22 '16 at 13:12
0

You can use onunload and onbeforeunload events to handle this but with the condition that they will fire even while navigating to other sites on clicking links

angular
  .module('app', [])
  .controller('AppController', function ($localStorage, $window) {
     $window.onunload = function () {
       $localStorage.setItem('LastLoggedOutTime', new Date());
     }
  });
Aditya Singh
  • 15,810
  • 15
  • 45
  • 67
0
angular.module('app', [])
    .controller('exitController', function($scope, $window) {
        $scope.onExit = function() {
            // your function
        };

        $window.onbeforeunload =  $scope.onExit;
});
Engin
  • 755
  • 7
  • 20