Your model has an error. At the first time, you save a primitive value. Then, you want to "append" another value to it.
Seems like you actually want to use an object: var myObj = localStorage. GetItem("myName"); if(myObj) myObj = JSON. Parse(myObj); //Variable exists else myObj = {}; //Elsem create a new object function appendNewValue(name, value){ myObjname = value; localStorage.
SetItem("myName", JSON. Stringify(myObj)); /* Saves data immediately. Instead of saving every time, you can also add this persistence feature to the `(before)unload` handler.
*/ }.
As mentioned above I was spoilt for choice on this one. Thanks for this answer - but also liked the one from @ampersand. – T9b Sep 25 at 20:57.
First of all, you don't want to keep writing to/from localStorage everytime a link is clicked, because this'll slow down your page. Keep an updated Array populated with the element ids, then write to localStorage before the user navigates away from the page (by binding to the window's onbeforeunload event, for instance). First: var clickedLinks = ; // this Array will hold the ids of the clicked links function uniqueClick(id){ return!
~clickedLinks. IndexOf(id); // this tests whether the id is already in the Array }; In your click handler: if(uniqueClick(this. Id)){ clickedLinks.
Push(this. Id); // append the new element id to the Array } Bind to window. Onunload to save the Array before the user navigates from the page: window.
Onunload = function(){ localStorage. SetItem('clickedLinks',JSON. Stringify(clickedLinks)); // stringify the Array and save to localStorage } To retrieve clickedLinks on subsequent page visit: // convert the String back to an Array; try/catch used here in case the value in localStorage is modified and unable to be parsed, in which case clickedLinks will be initialized to an empty Array try{ var clickedLinks = JSON.
Parse(localStorage. GetItem('clickedLinks')) || ; }catch(e){ var clickedLinks = ; } You may want to replace the first line (var clickedLinks = ;) with this last bit of code, as it will initialize the Array if it doesn't exist. UPDATE: IE8 does not support Array.indexOf.
Alternatives might be: use jQuery's $. InArray by replacing! ~clickedLinks.
IndexOf(id); with! ~$. InArray(id, clickedLinks); Detect whether Array.prototype.
IndexOf is supported. If not, shim it with the code provided on this page.
This is a really great answer - if I was going to use an array. However, @Rob W just beat you by the use of an object. After testing this does not endlessly add values; if one exists with an identical key it appears to either transparently fail or just replace the values.
Shame I can't accept two answers because both work. – T9b Sep 25 at 20:56.
I suggest to define in your code this: localStorage. Set= function(key,val) { localStorage. SetItem(JSON.
Stringify(val)); } localStorage. Get = function(key,defval) { var val = localStorage. GetItem(key); if( typeof val == "undefined" ) return defval; return JSON.
Parse(val); } and use them instead of get/setItem. They will give you ready to use JS values that you can use in the way you need.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.