admin管理员组文章数量:1426066
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.
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.
Share Improve this question edited Jan 19, 2016 at 11:59 durga siva kishore mopuru asked Jan 19, 2016 at 11:25 durga siva kishore mopurudurga siva kishore mopuru 1,3576 gold badges34 silver badges57 bronze badges 5-
Do you need this to work across different windows/tabs? If not, you should use
sessionStorage
to begin with. – C3roe Commented Jan 19, 2016 at 11:55 - no only one single tab – durga siva kishore mopuru Commented Jan 19, 2016 at 11:57
-
Well then use
sessionStorage
– it only lives until the window/tab is closed. – C3roe Commented Jan 19, 2016 at 11:59 - Thank u @CBroe. Now my issue was solved using sessionStorage – durga siva kishore mopuru Commented Jan 20, 2016 at 3:20
- OK, I added that as an answer, plus a little more context. – C3roe Commented Jan 20, 2016 at 8:37
2 Answers
Reset to default 5If 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
Maybe that's you're looking for Identifying Between Refresh And Close Browser Actions
本文标签: javascriptHow to differentiate browser close and refresh using angularjsStack Overflow
版权声明:本文标题:javascript - How to differentiate browser close and refresh using angularjs? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745421575a2657922.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论