基于'sessionStorage'与'userData'的类session存储
Storage.js:
注意:此版本实现的存储在符合Web存储标准(ie8及ie8以上的版本与其他主流浏览器)的情况下与session的周期一致,但在页面不关闭的情况下没有过期时间,ie7及以下版本则默认是永久保存,但可以通过预留的方法setMaxAge(Number age)来设置有效期,设置0的话在关闭或刷新页面时会清除缓存。
(function initStorageClass(win){
var inherit=function(o){
if(o===null || o ===undefined) throw TypeError();
if(Object.create) return Object.create(o);
var t = typeof o;
if(t!=='object'&&t!=='function') throw TypeError();
function f(){}
f.prototype=o;
return new f();
};
var extend=function(a,b){
for ( var key in b) { a[key]=b[key]; }
return a;
};
var defineSubclass=function(superclass,constructor,methods,statics){
constructor.prototype=inherit(superclass.prototype);
constructor.prototype.constructor=constructor;
if(methods) extend(constructor.prototype,methods);
if(statics) extend(constructor,statics);
return constructor;
};
Function.prototype.extend=function(constructor,methods,statics){
return defineSubclass(this,constructor,methods,statics);
};
// 创建一个抽象类
var AbstractStorage=function AbstractStorage(){
throw new Error('Can\'t create abstract class instance');
};
// 添加抽象类的实例方法(已实现)
extend(AbstractStorage.prototype,{
setItem:function(k,v){
k=encodeURIComponent(k);
v=encodeURIComponent(v);
this.storage.setItem(k,v);
return this;
},
getItem:function(k){
k=encodeURIComponent(k);
return decodeURIComponent(this.storage.getItem(k));
},
removeItem:function(k){
k=encodeURIComponent(k);
this.storage.removeItem(k);
return this;
},
setMaxAge:function(age){ // 为IE的userData版本预留了设置有效期的方法
if(isNaN(age)) throw new TypeError('userData\' max-age must be a number,but '+age+' is not a number');
if(this.model&&this.model==='userData') {
var now=new Date().getTime();
var expires=now+age*1000;
this.storage.expires=new Date(expires).toUTCString();
} else {
throw new Error('sessionStorage did\'t support set max-age。');
}
return this;
}
});
var Storage=null;
if(win.Storage) {// 实现了Web存储标准的浏览器
Storage=AbstractStorage.extend(
function WebStorage(){
// IE中实现了Web存储标准的版本,在本地目录下无法使用sessonStorage
if(!win.sessionStorage) {
throw new Error('local web is can\'t save sessionStorage');
}
this.model='sessionStorage';
// 默认使用sessionStorage,也可以自己传入,model自行修改
this.storage=win.sessionStorage;
}
);
} else if(win.navigator.appVersion&&win.navigator.appVersion.indexOf('MSIE')>=0){
// 不支持web存储标准的IE浏览器(IE11的核心版本已和Netscape统一,IE8以上的支持web存储标准)
Storage=(AbstractStorage.extend(
function IEStorage(maxAge){
this.model='userData';
this.maxAge=maxAge;
this.storage=(function initUserData(t){
var memory = document.createElement('div');
memory.style.display='none';
//附加userData行为
memory.style.behavior='url("#default#userData")';
document.appendChild(memory);
if(t.maxAge) {// 设置userData有效期,默认永久,单位毫秒
var now=new Date().getTime();
var expires=now+t.maxAge*1000;
memory.expires=new Date(expires).toUTCString();
}
memory.load('UserDataStorage'); //载入存储的
extend(memory,{
setItem:function(k,v){
this.setAttribute(k,v);
this.save('UserDataStorage');
return this;
},
getItem:function(k){
return this.getAttribute(k)||null;
},
removeItem:function(k){
this.removeAttribute(k);
this.save('UserDataStorage');
return this;
}
});
return memory;
}(this));
}
));
}
win.IStorage=Storage;
win.memory=new Storage()||null;// 创建一个实例对象,可以在脚本中直接引用
}(window));
index.html(简单测试):
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="Storage.js"></script>
<script type="text/javascript">
window.onload=function(){
memory.setItem('test','success');
alert(memory.getItem('test'));
};
</script>
</head>
<body> </body>
</html>
在HTML页面中引用Storage.js文件,可以在宿主环境中直接使用已经生成的实例memory(window.memory)。也可以自己创建一个新实例new IStorage()
memory.setItem('test','success'); // add
alert(memory.getItem('test')); // select
memory.removeItem('test'); // delete
适用实现了Web存储标准的浏览器(Storage)与IE浏览器(userData),userData的生命周期请自行根据项目进行设置。
基于'sessionStorage'与'userData'的类session存储的更多相关文章
- [转]mvc3 使用session来存储类来存储用户登陆信息
mvc3 使用session来存储类来存储用户登陆信息 2013-08-26 09:48:56| 分类: NET开发 |举报 |字号 订阅 项目之前的登陆机制是这样的:用户登陆后初始化一个类,类 ...
- C# redis 分布式session存储
https://github.com/uliian/SessionExtentionStore 一个基于Redis的Session存储扩展方案,解决ASP.NET中Session的局限性和跨应用程序使 ...
- [安卓] 12、开源一个基于SurfaceView的飞行射击类小游戏
前言 这款安卓小游戏是基于SurfaceView的飞行射击类游戏,采用Java来写,没有采用游戏引擎,注释详细,条理比较清晰,适合初学者了解游戏状态转化自动机和一些继承与封装的技巧. 效果展示 ...
- Jetty集群配置Session存储到MySQL、MongoDB
在Web开发中,Session表示HTTP服务器与客户端(例如浏览器)的“会话”,每个客户端会有其对应的Session保存在服务器端,通常用来保存和客户端关联的一些信息,例如是否登录.购物车等. Se ...
- Jetty容器集群配置Session存储到MySQL、MongoDB
在Web开发中,Session表示HTTP服务器与客户端(例如浏览器)的"会话",每个客户端会有其对应的Session保存在服务器端,通常用来保存和客户端关联的一些信息,例如是否登 ...
- 基于MongoDB打造.Net的分布式Session子系统
基于MongoDB打造.Net的分布式Session子系统 Taobao有她自己的分布式session框架,.net阵营也不能落后了,在下做了个基于MongoDB的支持最多26台MongoDB的分布式 ...
- 分布式集群下的Session存储方式窥探
传统的应用服务器,自身实现的session管理是大多是基于单机的,对于大型分布式网站来说,支撑其业务的远远不止一台服务器,而是一个分布式集群,请求在不同的服务器之间跳转.那么,如何保持服务器之前的se ...
- NET Core2.0 Memcached踩坑,基于EnyimMemcachedCore整理MemcachedHelper帮助类。
DotNetCore2.0下使用memcached缓存. Memcached目前微软暂未支持,暂只支持Redis,由于项目历史原因,先用博客园开源项目EnyimMemcachedCore,后续用到的时 ...
- Flask 框架中 上下文基础理念,包括cookie,session存储方法,requset属性,current_app模块和g模块
Flask中上下文,分为请求上下文和应用上下文.既状态留存 ,就是把变量存在某一个地方可以调用 请求上下文:实际就是request和session用法理念,既都是可以存储东西. 应用上下文:既变量共享 ...
随机推荐
- 【原创】Freak3D printer 的Repetier-Host 的设置
软件版本号:Repetier-Host 版本号 1.6.1 操作系统: win10 x64 专业版 3d打印机: Freak3D: 1. freak3D的相关参数(通过freak3D的官方打印文件得到 ...
- polygonZM---> poliygon
ArcToolbox > Conversion Tools > To Shapefile > Feature Class To Shapefile (multiple) Clic ...
- U盘装系统系列一—-安装老毛桃U盘启动制作工具
今天跟大家分享下如何制作U盘启动盘,通过U盘启动来安装操作系统.U盘便于携带,同时能解决光驱出问题装不了系统的麻烦,可谓是装机利器!我一直用的都是老毛桃的U盘启动制作工具,很好用,很强大,就以它来演示 ...
- Python入门-----介绍
摘要:Python语言的特点 ----->优雅.明确.简单 一.Python适合的领域 web网站和各种网络服务 系统工具和脚本 作为“胶水”语言,把其他语言开发的模块包装起来方便使用 二.Py ...
- C语言 之 printf () 函数你真的会用吗?
main(){ int i=8; printf("%d %d %d %d %d %d ",++i,--i,i++,i--,-i++,-i--); } 运行结果 8 7 7 8 -7 ...
- RichTextBox控件日常使用集合
1.RichTextBox控件自动滚动到底部 richTextBox1.ScrollToCaret(); //将控件的内容滚动到当前光标位置
- IOS 客户端测试入门.pdf
IOS 客户端测试入门 http://www.open-open.com/doc/view/42d1257bf67946f595e843bfdbdfeabf
- ubuntu安装openssh-server
openssh-server是依赖于openssh-clienr的,那ubuntu不是自带了openssh-client吗? 原因是自带的openssh-clien与所要安装的openssh-serv ...
- Qt编程之数据流图(dataflow diagram)的编写
不知道怎么搞. 在网上搜了一些资料,说是有提供的Demo样例 https://forum.qt.io/topic/18472/dataflow-programming-gui/4 http://sta ...
- ActionBarActivity & FragmentActivity
1 ActionBarActivity 是FragmentActivity的一个子类 2 ActionBarActivity 加入了对actionBar的操作, 比如getSupportActionB ...