博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
封装cookie localStorage sessionStorage
阅读量:6914 次
发布时间:2019-06-27

本文共 2731 字,大约阅读时间需要 9 分钟。

var cookie = function(name, value, options) {        if (typeof value !== 'undefined') {            options = options || {};            if (value === null) {                value = '';                options = $.extend({}, options);                options.expires = -1;            }            var expires = '';            if (options.expires && (typeof options.expires === 'number' || options.expires.toUTCString)) {                var date;                if (typeof options.expires === 'number') {                    date = new Date();                    date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));                } else {                    date = options.expires;                }                expires = '; expires=' + date.toUTCString();            }            var path = options.path ? '; path=' + (options.path) : ';path=/';            var domain = options.domain ? '; domain=' + (options.domain) : '';            var secure = options.secure ? '; secure' : '';            document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');        } else {            var cookieValue = null;            if (document.cookie && document.cookie !== '') {                var cookies = document.cookie.split(';');                for (var i = 0; i < cookies.length; i++) {                    var cookie = $.trim(cookies[i]);                    if (cookie.substring(0, name.length + 1) === (name + '=')) {                        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));                        break;                    }                }            }            return cookieValue;        }    }    var removeCookie = function(key) {        $.cookie(key, '', {            expires: -1        });    }
var storage = function(st, key, value, expires) {            if (st == 'l') {                st = window.localStorage;                expires = expires || 60;            } else {                st = window.sessionStorage;                expires = expires || 5;            }            if (typeof value != 'undefined') {                try {                    return st.setItem(key, JSON.stringify({                        data: value,                        expires: new Date().getTime() + expires * 1000 * 60                    }));                } catch (e) {}            } else {                var result = JSON.parse(st.getItem(key) || '{}');                if (result && new Date().getTime() < result.expires) {                    return result.data;                } else {                    st.removeItem(key);                    return null;                }            }        }

 

转载于:https://www.cnblogs.com/xlljay/p/6272326.html

你可能感兴趣的文章
java获取访问路径、域名、项目名、请求入参
查看>>
nginx:not a directory
查看>>
【M9】利用destructors避免泄漏资源
查看>>
怎样让操作系统的虚拟机退出全屏?
查看>>
关于MySQL里的found_row()和row_count()解释及用法 [复制链接]
查看>>
SQL Server 2008 R2 导出数据脚本的方法
查看>>
TCP的定时器
查看>>
php内核分析(六)-opcode
查看>>
twisted: echo server
查看>>
iOS地图的注释(Annotation)
查看>>
存储过程中递归调用
查看>>
android INSTALL_FAILED_INSUFFICIENT_STORAGE错误
查看>>
android开发之第三方集成之OAUTH教程篇
查看>>
spring+mybatis 多数据源整合
查看>>
HTML5 网络拓扑图整合 OpenLayers 实现 GIS 地图应用
查看>>
php 两种短网址生成方法
查看>>
AOP - PostSharp 2.0
查看>>
Spring测试框架JUnit4.4
查看>>
openSUSE 12.1下搭建Web服务器
查看>>
Contact Manager Web API 示例[2] Web API Routing
查看>>