views:

130

answers:

1

I did my own feature using this:

function save(title, url)
{
 for (var i = 1; i < localStorage.length; i++) 
 {
  localStorage["saved-title_" + i + ""] = title;
  localStorage["saved-url_" + i + ""] = url;
 } 
}

function listFavs()
{
 for (var i = 1; i < localStorage.length; i++) {
    console.log(localStorage["saved-fav-title_" + i + ""]);
 }
}

save() happens when someone clicks on this:

onclick="save(\'' + title + '\', \'' + tab.url + '\');"> ' + title + '</a>';

However... it doesn't show the saved localStorages, how am I supposed to make it work?

+1  A: 

Might it be because you are using the key 'saved-title_' + i to save the value and 'saved-fav-title_' + i to retrieve it?

The difference is the fav- part.

And your enumeration of the localStorage is bound to create errors as there is no guarantee that all items in it have a key that matches the pattern provided 'saved-fav-title_' + i - actually it is guaranteed to not be so as you are yourself inputting items with keys in the form of 'saved-url_'+ i.

So, if you want to properly enumerate the items with a key matching a pattern use

function listFavs(){
    var key;
    for (var i = 0, len = localStorage.length; i < len; i++){
        key = localStorage.key(i);
        if ((/^saved-fav-title_/).test(key)) {
            console.log(localStorage.getItem(key);
        }
   }
}
Sean Kinsey
console.log(localStorage[key]); comes up with an error in Dreamweaver and no, it does not work.
Blondie
There, the code is now updated to work according to the spec http://dev.w3.org/html5/webstorage/#the-localstorage-attribute. The first one would only work in Chrome.
Sean Kinsey