cookie代替localstorage 【draft】

2017-04-11  本文已影响67人  编码的哲哲
            getItem = function () {
                if (document.cookie.length>0) {
                    let c_start=document.cookie.indexOf(name + '=')
                    if (c_start!=-1) { 
                        c_start=c_start + name.length+1 
                        c_end=document.cookie.indexOf(';',c_start)
                        if (c_end==-1) c_end=document.cookie.length
                        return unescape(document.cookie.substring(c_start,c_end))
                    } 
                }
                return '';
            };
            setLocalStorage = function (opened) {
                var value = JSON.stringify({opened: opened, timestamp: new Date().getTime()});
                var name = "kjl_case_guide";
                if (getItem(name) === '') {
                    document.cookie += '; ' + name + '=' + value;
                } else {
                    let c_start=document.cookie.indexOf(name + '=') + name.length + 1;
                    let preString = document.cookie.substr(0, c_start);
                    let c_end=c_end=document.cookie.indexOf(';',c_start);
                    let tailString = document.cookie.substr(c_end, document.cookie.length);
                    document.cookie = c_start + value + tailString;
                }
            };
        }
上一篇下一篇

猜你喜欢

热点阅读