zoukankan      html  css  js  c++  java
  • 本地存储

    一、cookie

    //设置cookie
    function setcookie(name,value){
        var iDate=10*24*60*60*1000;
        var oDate = new Date;
        oDate.setDate(oDate.getDate() + iDate);
        document.cookie = name+'='+value+';expires='+oDate;
    }
    //获取cookie
    function getcookie(name){
        var str = document.cookie;
        var arr = str.split('; ');
        for(var i=0;i<arr.length;i++){
            var arr1 = arr[i].split('=');
            if(arr1[0] == name){
                return arr1[1];
            }
        }
        return '';
    }
    //删除cookie
    function removecookie(name){
        setcookie(name, "", -1);
    }
    

      二、localStorage

    存储到本地:window.localStorage.setItem(key,value)
    获取本地存储:window.localStorage.getItem(key)
    清除本地缓存:window.localStorage.removeItem(key)

    三、因为safari浏览器无痕模式,对localStorage不兼容,可以用cookie

    //判断是否支持localStorage
    function isLocalStorageSupport(){
        try {
            var isSupport = 'localStorage' in window && window['localStorage'] !== null;
            if (isSupport) {
                localStorage.setItem('__test', '1');
                localStorage.removeItem('__test');
            }
            return isSupport;
        } catch (e) {
            return false;
        }
    }
    

     

    //获取本地存取
    function getAccountInfo(key){
        if(isLocalStorageSupport()){
            return window.localStorage.getItem(key);
        }else if(window.navigator.cookieEnabled){
            return getcookie(key);
        }
        return null
    }
    //存储到本地
    function setAccountInfo(key,value){
        if(isLocalStorageSupport()){
             return  window.localStorage.setItem(key,value);
        }else if(window.navigator.cookieEnabled){
             return setcookie(key,value);
        }
        return null
    }
    
    //清除本地存储
    function clearAccountInfo(key){
        if(isLocalStorageSupport()){
            return  window.localStorage.removeItem(key);
        }else if(window.navigator.cookieEnabled){
            return removecookie(key);
        }
        return null
    }
    

      

     

  • 相关阅读:
    jenkins as code 与go语言学习
    VC++ 网络编程总结(一)
    Linux下的C高级编程---学习
    面试前必做4准备
    MFC视图切换大全总结
    多线程编程技术学---学习笔记--线程编程基础知识
    C语言高效编程的几招(绝对实用,绝对经典)
    汇编语言(学习笔记-----[bx]和loop)
    汇编语言(学习笔记----源程序)
    汇编语言(学习笔记----寄存器-内存访问)
  • 原文地址:https://www.cnblogs.com/karila/p/7099592.html
Copyright © 2011-2022 走看看