Save localstorage

2019-04-05  本文已影响0人  iceoreocheese

LocalStorage

含义

判断浏览器是否支持localStorage属性

if(!window.localStorage){
   alert("浏览器支持localstorage");
   return false;
 } else {
   // some code here...
 }

Localstorage的方法

1.添加新的数据到Localstorage中存储

localStorage.setItem(keyName, keyValue); 
            if(!window.localStorage){
            alert("浏览器支持localstorage");
            return false;
        }else{
            var storage=window.localStorage;
            //写入a字段
            storage["a"]=1;
            //写入b字段
            storage.a=1;
            //写入c字段
            storage.setItem("c",3);
            console.log(typeof storage["a"]);
            console.log(typeof storage["b"]);
            console.log(typeof storage["c"]);
        }

2.返回指定keyName的Localstorage中存储的值

localStorage.getItem(keyName); 

3.从Localstorage中移除指定keyName的数据项

localStorage.removeItem(keyName); 

4.清除所有的本地Localstorage存储

localStorage.clear(); 

localStorage的优劣

1.优势

2.劣势

上一篇下一篇

猜你喜欢

热点阅读