zoukankan      html  css  js  c++  java
  • localstorage和sessionstorage上手使用记录

    通过阅读各路大神对web存储locastorage和sessionstorage的用法解析,自己试用了一下,在此留个备忘。

    在项目中,如果用到很多次storage,要存储很多数据,就要把它封装成函数了:

    (该函数系不知名大神所写,如有侵犯原创,请联系我……)

    function setStorage(key,value){
            if(!window.localStorage){
                alert("浏览器不支持localstorage");
                return false;
            }else{
                var storage=window.localStorage;
                //写入字段
                storage.setItem(key,value);
            }
        }
        function getStorage(key){
            if(!window.localStorage){
                alert("浏览器不支持localstorage");
            }else{
                var storage=window.localStorage;
                var key=storage.getItem(key);
    //            console.log(key);
                return key;
            }
    }

    setStorage是存储数据的,key是指定的数据名称,可以随意起,但是一定要是字符串类型,否则浏览器自动把值作为key的名字。

    如图 第一个值,就是key不是以字符串指定的,即没有加双引号。

    value值字符串类型的也切记加双引号。

    在浏览器中如何查看storage?

     

    较新版本的chrome浏览器,查看位置如图:

    在项目中如果多次调用同样的存储数据的函数,则数据会实时改变,如果需要清除所有存储的数据:

    localstorage.clear();或者sessionStorage.clear();

    项目中使用的原则就是,哪个数据需要存储,就用哪个数据调用存数数据的函数。

    其他概念,相信其他参考资料已经写的很详细透彻了。

    http://www.111cn.net/wy/html5/85886.htm

    上面这篇写的不错,收藏一下。

  • 相关阅读:
    openwrt 汉化
    错误: libstdc++.so.6: cannot open shared object file: No such file or directory
    openwrt uci
    openwrt makefile选项
    Ubuntu服务器断网问题解决
    lldpcli 常用命令
    openwrt ramips随记
    shell脚本学习(二)
    完成响应式的方式
    盒子模型 W3C中和IE中盒子的总宽度分别是什么
  • 原文地址:https://www.cnblogs.com/beileixinqing/p/6893492.html
Copyright © 2011-2022 走看看