admin管理员组文章数量:1023187
Hi I have used localStorage to store the value of a variable.The problem is that localStorage never resets from what I managed to find on the internet.I am interested to reset localStorage when the browser is closed.
Is there a way to do that?
I have tryed using $(window).unload from jQuery but that detects changes related to the window and it resets the localStorage variable to soon.
Hi I have used localStorage to store the value of a variable.The problem is that localStorage never resets from what I managed to find on the internet.I am interested to reset localStorage when the browser is closed.
Is there a way to do that?
I have tryed using $(window).unload from jQuery but that detects changes related to the window and it resets the localStorage variable to soon.
Share Improve this question asked Jun 2, 2012 at 17:04 Nistor AlexandruNistor Alexandru 5,3939 gold badges50 silver badges71 bronze badges 3-
6
Why would you be using
localStorage
to store a value that you DON'T want to persist? – mVChr Commented Jun 2, 2012 at 17:07 - I only the value to exist until the browser is closed – Nistor Alexandru Commented Jun 2, 2012 at 17:10
- Why not just store the data in a session cookie which is automatically destroyed when the browser is closed? – jfriend00 Commented Jun 2, 2012 at 17:13
2 Answers
Reset to default 2Read my answer in which I explained how to detect browser close, $(window).unload doesn't work always.
<script>
window.onbeforeunload = function (e) {
if (localStorage) {
localStorage.clear();
}
};
</script>
But, yes I would remend to use Cookies or session variable which would be destroyed on browser close instead of localStorage.
You probably want to use window.sessionStorage instead.
sessionStorage
This is a global object (sessionStorage) that maintains a storage area that's available for the duration of the page session. A page session lasts for as long as the browser is open and survives over page reloads and restores. Opening a page in a new tab or window will cause a new session to be initiated.
Hi I have used localStorage to store the value of a variable.The problem is that localStorage never resets from what I managed to find on the internet.I am interested to reset localStorage when the browser is closed.
Is there a way to do that?
I have tryed using $(window).unload from jQuery but that detects changes related to the window and it resets the localStorage variable to soon.
Hi I have used localStorage to store the value of a variable.The problem is that localStorage never resets from what I managed to find on the internet.I am interested to reset localStorage when the browser is closed.
Is there a way to do that?
I have tryed using $(window).unload from jQuery but that detects changes related to the window and it resets the localStorage variable to soon.
Share Improve this question asked Jun 2, 2012 at 17:04 Nistor AlexandruNistor Alexandru 5,3939 gold badges50 silver badges71 bronze badges 3-
6
Why would you be using
localStorage
to store a value that you DON'T want to persist? – mVChr Commented Jun 2, 2012 at 17:07 - I only the value to exist until the browser is closed – Nistor Alexandru Commented Jun 2, 2012 at 17:10
- Why not just store the data in a session cookie which is automatically destroyed when the browser is closed? – jfriend00 Commented Jun 2, 2012 at 17:13
2 Answers
Reset to default 2Read my answer in which I explained how to detect browser close, $(window).unload doesn't work always.
<script>
window.onbeforeunload = function (e) {
if (localStorage) {
localStorage.clear();
}
};
</script>
But, yes I would remend to use Cookies or session variable which would be destroyed on browser close instead of localStorage.
You probably want to use window.sessionStorage instead.
sessionStorage
This is a global object (sessionStorage) that maintains a storage area that's available for the duration of the page session. A page session lasts for as long as the browser is open and survives over page reloads and restores. Opening a page in a new tab or window will cause a new session to be initiated.
本文标签: jqueryJavascript browser close eventStack Overflow
版权声明:本文标题:jquery - Javascript browser close event - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745517811a2154159.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论