抛弃cookie使用浏览器本地存储实现浏览记录
抛弃cookie使用浏览器本地存储实现浏览记录。
1,首先下载localforage.min.js这个组件,而后在引入进来(如果浏览器不支持indexdb,会自动用localStorage保存)localStorage保存一般为5m,而indexdb本地数据库支持上GB的存储。
2,以浏览文章为例,首先得在公用JS文件写入INDEXdb大数据库配置
1,首先下载localforage.min.js这个组件,而后在引入进来(如果浏览器不支持indexdb,会自动用localStorage保存)localStorage保存一般为5m,而indexdb本地数据库支持上GB的存储。
2,以浏览文章为例,首先得在公用JS文件写入INDEXdb大数据库配置
- localforage.config({driver: localforage.INDEXEDDB, // 使用 IndexedDBname: 'meiwenyyuqi', // 数据库名称version: 1.0, // 数据库版本storeName: 'meiweny' // 存储对象的名称});
- //保存阅读记录function set_history(_url, _name, _id) {var yueduhistorytime = new Date().formatCode();var userpic = $('.userinfos .userface').attr('src');/var username = $('.userinfos .username').text();var user = {id: _id,name: _name,url: _url,yueduhistorytime: yueduhistorytime,username:username,userpic: userpic,};localforage.getItem('yuedu_news_History').then(function(Game_Data) {Game_Data = Game_Data || [];var userExists = Game_Data.some(function(item) {return item.name === _name;});if (!userExists) {Game_Data.push(user);localforage.setItem('yuedu_news_History', Game_Data).then(function() {localStorage.setItem('duanpian_history_Length', Game_Data.length);}).catch(function(err) {console.error("Error setting item", err);});} else {console.log("User already exists");}}).catch(function(err) {console.error("Error getting item", err);});}
- //获取阅读记录function get_history(limit = 10) {return localforage.getItem('yuedu_news_History').then(function(Game_Data) {Game_Data = Game_Data || [];var yueduHistory = Game_Data.slice().reverse();/var topRecords = yueduHistory.slice(0, limit);var aaastr = '';topRecords.forEach(function(item) {aaastr += '<li><a class="meiwen" href="' + item.url + '" title="' + item.name + '">' + item.name + '</a><span class="time">' + yuedu_riq + '</span></li>';});return aaastr;}).catch(function(err) {console.error("Error getting item", err);return ''; });}