zoukankan      html  css  js  c++  java
  • 浏览器的 local storage

    浏览器  

    1. local storage      本地存储
    2. session storage    会话存储
    3. cookies                  本地存储
    4. 1.     local storage

    local Storage 是解决cookies存储空间不足问题.

    cookle中每条cookie的存储空间为4K.

    local storage 的存储空间一般为5M.这个根据浏览器变化.

    local storage 的值类型限定为string类型.如果存储内容多的话会导致页面变卡.

    local storage 属于永久性存储.

    1. 2.     local storage 的使用

    1° 判断浏览器是否支持

    if(!window.localStorage){

                alert("浏览器支持localstorage");

                return false;

            }else{

                //主逻辑业务

            }

    2° local storage 的写入

    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"]);

            }

    3° local storage 的读取

    if(!window.localStorage){

                alert("浏览器支持localstorage");

            }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"]);

                //第一种方法读取

                var a=storage.a;

                console.log(a);

                //第二种方法读取

                var b=storage["b"];

                console.log(b);

                //第三种方法读取

                var c=storage.getItem("c");

                console.log(c);

            }

    4° local storage的删除

    1>清除所有

    var storage=window.localStorage;

                storage.a=1;

                storage.setItem("c",3);

                console.log(storage);

                storage.clear();

                console.log(storage);

    2>删除某个键

    var storage=window.localStorage;

                storage.a=1;

                storage.setItem("c",3);

                console.log(storage);

                storage.removeItem("a");

                console.log(storage.a);

    5° local storage 的键获取

    var storage=window.localStorage;

                storage.a=1;

                storage.setItem("c",3);

                for(var i=0;i<storage.length;i++){

                    var key=storage.key(i);

                    console.log(key);

                }

  • 相关阅读:
    springmvc的单文件上传
    使用Eclipse创建maven项目
    @responseBody注解的使用
    Oracle-怎么在表的特定位置增加列
    Oracle-创建新表,创建备份表,对表中插入多条数据
    EXCEL-排名前三名显示小红旗,后三名显示小黑旗
    Hive-insert into table 与 insert overwrite table 区别
    数仓工具介绍
    Hive-删除表(drop、truncate的区别)
    EXCEL-批量修改列宽
  • 原文地址:https://www.cnblogs.com/enych/p/10499513.html
Copyright © 2011-2022 走看看