1

I'm trying to get the favorites list using localstorage.

I have a listview with a number of items. When I click on an item it opens up and shows some data.

In the same page I have a button with the text Bookmark. When I click on it, that particular page has to be added to that favourites list . When I'm trying to get the list of favourites it shows up empty.

Here is my Fiddle: http://jsfiddle.net/nPwXK/1

Thanks in Advance.

Omar
  • 32,302
  • 9
  • 69
  • 112
Lucky
  • 425
  • 3
  • 8
  • 19

1 Answers1

0

Okay so :

  • define the prefix somewhere.
  • localStorage use a Map to store data so when you store something it's a key/value pair.
  • Your indexOf() is not complete
  • you have infinite recursion in fromStorage()
  • open the console...........!!!

localStorage.setItem(key, value)

localStorage.getItem(key)

You cannot do getItem(0) unless you do setItem(0) which you don't. But you do this :

for (var i = 0; i < localStorage.length; i++){
    localStorage.getItem(localStorage.key(i));
}

http://jsfiddle.net/techunter/nPwXK/11/

You still have some big code readability issue and weird code but it's not the topic here as your localStorage is now working

TecHunter
  • 6,091
  • 2
  • 30
  • 47
  • btw, I found out this topic with a little search on SO : http://stackoverflow.com/questions/3138564/looping-through-localstorage-in-html5-and-javascript – TecHunter Jul 17 '13 at 09:14
  • localstorage is working but i have one doubt..here i'm appending to the favorites list why it is not displaying like as in the home page. – Lucky Jul 17 '13 at 09:23
  • and after adding to favorites list when i close the browser.If i opened again the previous added one's are lost. – Lucky Jul 17 '13 at 09:29
  • @user2384323 where do you test? I don't think localStorage can work on a framed jsfiddle – TecHunter Jul 17 '13 at 11:58
  • i tested in browser(firefox)..even i tried in fiddle also once it stored..if i closed and opened again nothing is there in favorites list..i tried in fiddle made by you also..its adding unwanted data also. – Lucky Jul 17 '13 at 12:05
  • @user2384323 like I said. make a real html page not a jsfiddle. your browser won't know how to fetch cache of a dynamic content. – TecHunter Jul 17 '13 at 12:07
  • yes i made a real html page..i tried like index.html and my js file. – Lucky Jul 17 '13 at 12:10
  • @user2384323 [read this](https://developer.mozilla.org/en-US/docs/Web/Guide/DOM/Storage?redirectlocale=en-US&redirectslug=DOM%2FStorage) and you need a web server to test it. Allow cookies too. – TecHunter Jul 17 '13 at 13:34
  • i tried localstorage in the browser and its working but when i click on the particular item in the listview its storing with the last item name but not the clicked one – Lucky Jul 19 '13 at 07:01
  • @user2384323 your code is messy, you add click event on a class inside a loop that's why you have multiple bookmarks and stuff. Do this step by step, first construct the list then when it's done bind the events, make sure you don't bin them more than once. but it's off topic already. if localstorage is working accept this answer plz. – TecHunter Jul 19 '13 at 07:18
  • but by using your answer data is storing but when closed the browser and opened all the data is losting..its again fresh page – Lucky Jul 19 '13 at 08:34
  • 1
    http://thedevstop.wordpress.com/2011/09/13/getting-started-with-html5-local-storage/ – TecHunter Jul 19 '13 at 09:07
  • @TecHunter..Thank u for giving me your valuable suggestion. – Lucky Jul 19 '13 at 09:35