Angular deleting cookies on exiting website

I am looking for an option to delete all cookies if the user closes the browser window / tag. Is there a chance to trigger an event or something else?

+3


source to share


1 answer


Try the following:

function clearCookies(){
   var cookies = document.cookie.split(";");
   for (var i = 0; i < cookies.length; i++){   
      var vals =  cookies[i].split("=");
      document.cookie = vals[0] + "=;expires=Thu, 21 Sep 1979 00:00:01 UTC;";                                
   }        
}
// you will need jquery for this
$(window).on('beforeunload', function () {
   clearCookies()
});

      

or the clear function in angular way:



function clearCookies(){
    angular.forEach($cookies, function (value, key) {
       $cookieStore.remove(key);
    }); 
}

      

But if you can use session cookies (without expiration), they are automatically deleted by the browser when the user closes the browser.

0


source







All Articles