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
Add a ment  | 

2 Answers 2

Reset to default 2

Read 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
Add a ment  | 

2 Answers 2

Reset to default 2

Read 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