adding and displaying favorite names list using jquery/ajax without database

若如初见. 提交于 2019-12-24 07:29:52

问题


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.


回答1:


You could use local storage 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 totalStorage or jstorage.



来源:https://stackoverflow.com/questions/12560992/adding-and-displaying-favorite-names-list-using-jquery-ajax-without-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!