简体   繁体   中英

How to differentiate browser close and refresh using angularjs?

I have localstorage for employeeid stored.When i refresh the browser get local storage value like employeeid based on employeeid get some data.when i close the browser i want to clear employeeid in local storage by using angularjs or javascript. I tried the following code.

window.onunload = close;
    function close() {
        //// do something...
        localStorage.clear();
        return null;
    }

In the above code when i refresh the browser that time also window.onunload fired and clear the local storage values, but i want to clear local storage at the time of browser close only.

If you do not need this to work across different windows/tabs, then you should use sessionStorage instead of localStorage .

Where what you store into localStorage is “permanent”, sessionStorage stores values only until the window/tab is closed, similar to a “session cookie”, that is set without any lifetime – that will live only until the browser is closed (but will be available across different tabs when your site is open in more than one.)

A few more details can be found in these questions:
What is the difference between localStorage, sessionStorage, session and cookies?
HTML5 Local storage vs. Session storage

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM