首页 > 代码库 > 前端存储之cookie、localStorage

前端存储之cookie、localStorage

最近简单了解了前端存储中的cookie、localStorage两种存储方式

localStorage

window.localStorage.setItem(‘a‘, 1)   (window. 可省略)
localStorage.setItem(‘a‘, 1)          //存储数据
localStorage.getItem(‘a‘)             //获取数据
localStorage.removeItem(‘a‘)          //删除指定数据   
localStorage.clear( )            //删除全部数据

 

cookie

 

document.cookie = ‘name=1‘;            //设置cookie
console.log(document.cookie);           //获取cookie   
document.cookie = ‘name=1;expires=‘ + new Date(new Date().setTime(new Date().getTime()-1)).toGMTString();                            
                                    //通过过期时间删除cookie             

 

前端存储之cookie、localStorage