globalStorage与UserData客户端存储实现方案

作者:工具松鼠时间:2026-10-23 17:10:01浏览:449

本文介绍HTML5 globalStorage与IE UserData的兼容存储方案。globalStorage提供跨页面共享的大容量存储,但存在安全限制;UserData为IE特有。通过封装JavaScript类,统一处理两种机制的读写、属性管理及基于时间的过期策略,实现跨浏览器持久化存储。

globalStorage

globalStorage机制与域限制

HTML5规范中提出了globalStorage机制,其特点在于浏览器关闭后数据依然保留,且存储容量远超IE的UserData,单域限制为5120k。与sessionStorage类似,同一域下的所有页面均可共享该存储空间。


作用域


globalStorage['z.baidu.com']:允许z.baidu.com域名下的所有页面访问此存储空间。


globalStorage['baidu.com']:允许baidu.com域名下的所有页面访问此存储空间。


globalStorage['com']:允许所有com顶级域名下的页面共享此空间。


globalStorage['']:允许所有页面访问此公共空间。


目前Firefox仅支持当前域下的globalStorage存储。若尝试访问公共域空间,可能会触发“Security error”(错误码1000)。


过期时间


根据HTML5规范,globalStorage仅在涉及安全问题或用户明确要求时才会过期。浏览器应避免删除脚本正在访问的数据,且UserData应允许用户写入。


为了控制数据过期,脚本可在globalStorage的特定区域存储过期时间戳。在加载数据时判断是否过期,从而在一定程度上解决过期时间管理问题。


在存储数据时,同时存储对应的过期时间信息。


基于网络查询资料,为实现对非IE浏览器UserData的兼容,我对之前编写的代码进行了改进。


改进了原有的UserData(参考《UserData使用总结》)实现,现使其同时兼容IE及支持globalStorage的浏览器。


兼容IE与globalStorage的存储实现

具体代码如下:

function behaviorUserdata(udObj)
{ 
var me = this;
if(CMInfo.Bs_Name=='IE') //IE下用userdata实现客户端存储
    { 
var loaded = ''; //当前已载入的文件名
this.udObj = getObject(udObj);
this.udObj.style.behavior = 'url(#default#userdata)';
this.value = this.udObj.value;
this.inhtml = this.udObj.innerHTML;
//检查文件是否存在,存在est=undefined并返回true否则返回false
this.exist = function(filename){
try{
me.udObj.load(filename);//将文件名为 filename的 XML 载入
me.loaded = filename;
return true;
}catch(e){ return false;}
        } 
//预加载
this.preLoad = function(filename){
if(me.loaded=='' || me.loaded!=filename){me.exist(filename);}
return me.loaded;
        } 
//获取指定的属性值
this.getAtrib = function(filename,atrib){
if(me.preLoad(filename)!='')
            { 
var val = me.udObj.getAttribute(atrib);
return val==null?"":val;
}return "";
        } 
//移除对象的指定属性
this.remAtrib = function(filename,atrib){
me.udObj.removeAttribute(atrib);
me.udObj.save(filename); //将对象数据保存到名为filename的XML文件里面
return true;
        } 
//设置指定的属性值
this.setAtrib = function(filename,atrib,val,expire){
var etime = typeof(expire)=="undefined"?24*60*60:expire;
me.udObj.expires = me.setExpire(etime);
me.udObj.setAttribute(atrib,val);
me.udObj.save(filename);
        } 
//设置一个系列的对象数据(即整个XML文件)失效
this.remPartion = function(filename){
if(me.exist(filename))
            { 
me.udObj.expires = me.setExpire(-1);
me.udObj.save(filename);
            } 
        } 
//设置有效期
this.setExpire = function(sec){
var oTimeNow = new Date();
oTimeNow.setSeconds(oTimeNow.getSeconds() + parseInt(sec));
return oTimeNow.toUTCString();
        } 
}else //非IE下用globalStorage实现客户端存储
    { 
var domain = document.domain;
//获取指定的属性值
this.getAtrib = function(filename,atrib){
var oTimeNow = new Date();
var etime = parseInt(window.globalStorage[domain][filename + "__expire"]);
if(!etime || etime < parseInt(oTimeNow.getTime()))
            { 
me.remPartion(filename);
return '';
            } 
return window.globalStorage[domain][filename + "__" + atrib];
        } 
//移除对象的指定属性
this.remAtrib = function(filename,atrib){
try{window.globalStorage.removeItem(filename + "__" + atrib);}catch(e){}//删除
return true;
        } 
//设置指定的属性值
this.setAtrib = function(filename,atrib,val,expire){
var etime = typeof(expire)=="undefined"?24*60*60:expire;
window.globalStorage[domain][filename + "__expire"] = me.setExpire(etime);
window.globalStorage[domain][filename + "__" + atrib] = val;
        } 
//设置一个系列的对象数据失效
this.remPartion = function(filename){
me.remAtrib(filename,"expire");
return true;
        } 
//设置有效期
this.setExpire = function(sec){
var oTimeNow = new Date();
oTimeNow.setSeconds(oTimeNow.getSeconds() + parseInt(sec));
return oTimeNow.getTime();
        }     
    } 
}

其中CMInfo类见 一些常用的JS功能函数(一) (2009-06-04更新)


需要说明的是因为还没用到实际项目中,因此还不知其兼容性和稳定性如何,如果网友发现了BUG,还望指出。谢谢

作者最新文章