I need to keep track of a list of 25 favourites which are added from a list of ~100 entries. These favourites would be in a list of say
and I need them to be stored persistently. I also would require them to be able to be deleted and replaced with another favourite string value. I have looked at How do I store an array in localStorage? but that doesn't work for me because when I declare var names=[];
on the javascript file, every time my favourites function runs it redeclares the array and clears everything.
I have tried doing something like:
function addToFav(string)
{
if (localStorage.fav1) // if fav1 was created before
{
alert("local storage fav1 present, don't make variables");
}
else
{
alert('fav variables never made, make them now');
var i=1;
for (i=1; i<=25; i++)
{
var favNumber = "fav" + i;
localStorage.favNumber = "x";
}
alert(localStorage.fav1); // outputs "undefined"
}
}
it was my intention to make localStorage variables of fav1
, fav2
, fav3
... fav25
and then manage them individually. But this doesn't work since calling localStorage.favNumber = "x";
makes the local storage variable favNumber
equal to "x"
and not fav+i
.
I'm out of ideas at the moment; I have looked at http://playground.html5rocks.com/#async_transactions to try using a HTML5 database but I'm very new to web development and it seems a bit too much for what I'm trying to do. Does anyone know how to fix this issue? Any information would be helpful.
This might help. I use these to store info in Local Storage with cookie backup...
function setLocalStorage(c_name, value) {
var exdays = 30;
// if localStorage is present, use that
if (('localStorage' in window) && window.localStorage !== null) {
localStorage[c_name] = value;
} else {
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
document.cookie = c_name + "=" + c_value;
}
}
function getLocalStorage(c_name) {
// if localStorage is present, use that
if (('localStorage' in window) && window.localStorage !== null) {
return localStorage[c_name];
} else {
var i, x, y, ARRcookies = document.cookie.split(";");
for (i = 0; i < ARRcookies.length; i++) {
x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
x = x.replace(/^\s+|\s+$/g, "");
if (x == c_name) {
return unescape(y);
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.