
Question:
using my website people can search any names in world. users can add their particular name to favorite list. each user can see their favorite names list in another page(names those added by user in one session). can you suggest a best method to do this using jquery/ajax. Thankz.
Answer1:You could use <a href="http://www.w3.org/TR/webstorage/#the-localstorage-attribute" rel="nofollow">local storage</a> to save the data. It works as easy as this:
localStorage.setItem('key', 'value');
localStorage.getItem('key'); // returns 'value'
You could save the names using an incrementing key:
localStorage.setItem('name-' + (localStorage.length + 1).toString(), 'favName');
// Names stored as 'name-0', 'name1', ...
And then, to retrieve the list:
var names = new Array();
if (localStorage) {
if (localStorage.length) {
for (var i = 0; i < localStorage.length; i++) {
names[i] = localStorage.getItem('name-' + i.toString());
}
} else {
names[0] = 'You have no favorite names stored';
}
}
There are some plugins that provide fallbacks on browsers that don't support web storage (Even on ie6), like <a href="https://github.com/jarednova/jquery-total-storage" rel="nofollow">totalStorage</a> or <a href="http://www.jstorage.info/" rel="nofollow">jstorage</a>.