操作session、localStorage、sessionStorage
做个记录 jquery 操作session
// jquery.session.js 简单使用方法
添加数据
$.session.set('key', 'value')
删除数据
$.session.remove('key');
获取数据
$.session.get('key');
清除数据
$.session.clear();
//localStorage存值永久有效 function setItems(){ var user = {}; user.name = 'Adam Li'; user.age = 25; user.home = 'China'; localStorage.setItem('userinfo',JSON.stringify(user)); } //localStorage取值 function getItems(){ var data = JSON.parse(localStorage.getItem('userinfo')); console.log("name:"+data.name+'\r age:'+data.age+"\r home:"+data.home); } //localStorage删除指定键对应的值 function deleteItem(){ localStorage.removeItem('userinfo'); console.log(localStorage.getItem('userinfo')); }
if(window.sessionStorage){ alert('ok'); }else{ alert('fail'); } // 设置值 sessionStorage.setItem('key_a', 1); // 取值 var key_a = sessionStorage.getItem('key_a'); console.log(key_a); // 删除 sessionStorage.removeItem('key_a'); console.log(sessionStorage.getItem('key_a'));// null sessionStorage.setItem('key_b', 1); sessionStorage.setItem('key_c', 2); // 清除所有键值 sessionStorage.clear(); console.log(sessionStorage.key_b); console.log(sessionStorage.key_c);