已学的前端存储(学生)

localstorage 存储数据

创建方法

例:Window.localstorage.setItem('a',1);

获取方法

例:Window.localstorage.getItem('a');

删除方法

例:window.localStorage.removeItem('a');

前面的写的话太麻烦了 可以封装成一个方法

(function(win,doc){

var GD16 = {

Items:function (val){
if(typeof val == 'object'){
for(var i in val ){//创建
win.localStorage.setItem(i,val[i]);
}
}
else if(typeof val == 'string'){//获取
return win.localStorage.getItem(val);
}
else{
return console.error('参数只能为josn || 字符串');
}
},
/**
*删除
* @param val 也可以为空 val删除单个 空删除全部
*/
rem:function (val){
if(val){
win.localStorage.removeItem(val);
}else{
var json = win.localStorage;
for(var i in json){
win.localStorage.removeItem(i);
}
}
}
};

win.GD16 = GD16;
})(window,document);

可以在页面上直接调用 例:GD16.rem


cookie

必须要有服务器才能设置cookie

创建不带过期时间的cookie

document.cookie = 'name = hh';

 带过期时间的cookie
var date = new Date();
var d = new Date ( date.setTime(date.getTime() + 5000));以毫秒计算 5秒过期
document.cookie =" name = ahh;expires =" + d.toGMTString();必须要为时间对象

删除cookie可以直接设置当前时间
 document.cookie =" name = ahh;expires =" + new Date().toGMTString();


 
 


posted @ 2017-05-31 11:46  幸为天下客  阅读(176)  评论(0编辑  收藏  举报