How to delete a localStorage item when the browser window/tab is closed?

My Case: localStorage with key + value that should be deleted when browser is closed and not single tab. Please see my code if its proper and what can be improved: //create localStorage key + value if not exist if(localStorage){ localStorage.myPageDataArr={“name”=>”Dan”,”lastname”=>”Bonny”}; } //when browser closed – psedocode $(window).unload(function(){ localStorage.myPageDataArr=undefined; }); 23 Answers 23 should be … Read more

HTML5 Local storage vs. Session storage

Apart from being non persistent and scoped only to the current window, are there any benefits (performance, data access, etc) to Session Storage over Local Storage? 1Best Answer 11 localStorage and sessionStorage both extend Storage. There is no difference between them except for the intended “non-persistence” of sessionStorage. That is, the data stored in localStorage … Read more

What is the difference between localStorage, sessionStorage, session and cookies?

What are the technical pros and cons of localStorage, sessionStorage, session and cookies, and when would I use one over the other? 10 s 10 This is an extremely broad scope question, and a lot of the pros/cons will be contextual to the situation. In all cases, these storage mechanisms will be specific to an … Read more

How do I store an array in localStorage? [duplicate]

This question already has answers here: How to store objects in HTML5 localStorage (24 answers) Closed 8 years ago. If I didn’t need localStorage, my code would look like this: var names=new Array(); names[0]=prompt(“New member name?”); This works. However, I need to store this variable in localStorage and it’s proving quite stubborn. I’ve tried: var … Read more

How to Store Objects in HTML5 localStorage?

I’d like to store a JavaScript object in HTML5 localStorage, but my object is apparently being converted to a string. I can store and retrieve primitive JavaScript types and arrays using localStorage, but objects don’t seem to work. Should they? Here’s my code: var testObject = { ‘one’: 1, ‘two’: 2, ‘three’: 3 }; console.log(‘typeof … Read more