In Angular js How can i clear LocalStorage values when browser closed not browser tab closed.

Hi Sir,

In my Angular Application i used local storage. when i logged in the app then i closed the browser with out logoff.Then i again i open the browser  with my app URL.it is showing prev. logged in section only.i want when i closed the browser cross mark then it need to be clear . not closed when  i closed Browser tab.
bharath kuamr ReddyDOT NET DEVELOPERAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kanti PrasadCommented:
Hi

Try
localStorage.removeItem(key);
0
bharath kuamr ReddyDOT NET DEVELOPERAuthor Commented:
where i need to call that one.because in browser close event or in tab close event
0
Kanti PrasadCommented:
Hi

You can make use of the beforeunload event in JavaScript.

window.onbeforeunload = function() {
  localStorage.removeItem(key);
  return '';
};
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

bharath kuamr ReddyDOT NET DEVELOPERAuthor Commented:
But That above method will work work when browser closed event and tab closed tab as well.But my point is when i closed tab my local storage value i did not want to clear.only when i close browser close then it need to work like gmail
0
Kanti PrasadCommented:
Hi

try

$(window).unload(function(){
 localStorage.removeItem(key);
});
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alexandre SimõesManager / Technology SpecialistCommented:
I think you're addressing the issue in the wrong way.
You cannot rely on the window events to do this because if the browser crashes, or whatever happens that makes those events no to be triggered, you'll end up with the same problem.

What you need to do is to be able to identify that the application is starting, and if the localCache is present, discard/refresh it.
You're even using AngularJS, so if you have a Single-Page App, whenever the Angular app loads you can clean the LocalStorage. Just note that this will also happen if the user does Ctrl+F5.
0
bharath kuamr ReddyDOT NET DEVELOPERAuthor Commented:
$(window).unload(function(){
 localStorage.removeItem(key);
});

above one working fine.from my site i am navigating to other site then click back button Then my local storage is clearing but that is not happend.when i am click back to my site that logged in need to stay.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.