javascript globalStorage類代碼
更新時間:2009年06月04日 17:48:38 作者:
非IE瀏覽器“userdata”的解決方案
globalStorage
這個也是html5中提出來,在瀏覽器關(guān)閉以后,使用globalStorage存儲的信息仍能夠保留下來,并且存儲容量比IE的userdata大得多,一個域下面是5120k。和sessionStorage一樣,域中任何一個頁面存儲的信息都能被所有的頁面共享。
作用域
globalStorage['z.baidu.com'] 所有z.baidu.com下面的頁面都可以使用這塊空間
globalStorage['baidu.com'] 所有baidu.com下面的頁面都可以使用這塊空間
globalStorage['com']:所有com域名都可以 共享的使用這一塊空間
globalStorage[''] :所有頁面都可以使用的空間
現(xiàn)在Firefox只支持當(dāng)前域下的globalStorage存儲, 如果使用公用域會導(dǎo)致一個這樣一個類似的錯誤“Security error” code: “1000”。
過期時間
按照HTML5的描述,globalStorage只在安全問題或者當(dāng)用戶要求時才會過期,瀏覽器應(yīng)該避免刪除那些正在被腳本訪問的數(shù)據(jù),并且userdata應(yīng)該是用戶可寫的。
因此我們的腳本要能夠控制過期時間,可以在globalStorage的某個區(qū)域存儲過期時間,在load的時候判斷是否過期,可以在一定程度上解決過期時間的問題。
存儲時,同時存儲過期時間
以上是我從網(wǎng)上查詢到的資料,為了兼容非IE瀏覽器“userdata”,我改進(jìn)了之前我自己寫的一個
“userdata”(見 UserData使用總結(jié)) ,現(xiàn)在是兼容IE和支持globalStorage的瀏覽器了。
function behaviorUserdata(udObj)
{
var me = this;
if(CMInfo.Bs_Name=='IE') //IE下用userdata實(shí)現(xiàn)客戶端存儲
{
var loaded = ''; //當(dāng)前已載入的文件名
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;}
}
//預(yù)加載
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); //將對象數(shù)據(jù)保存到名為filename的XML文件里面
return true;
}
//設(shè)置指定的屬性值
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);
}
//設(shè)置一個系列的對象數(shù)據(jù)(即整個XML文件)失效
this.remPartion = function(filename){
if(me.exist(filename))
{
me.udObj.expires = me.setExpire(-1);
me.udObj.save(filename);
}
}
//設(shè)置有效期
this.setExpire = function(sec){
var oTimeNow = new Date();
oTimeNow.setSeconds(oTimeNow.getSeconds() + parseInt(sec));
return oTimeNow.toUTCString();
}
}else //非IE下用globalStorage實(shí)現(xiàn)客戶端存儲
{
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;
}
//設(shè)置指定的屬性值
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;
}
//設(shè)置一個系列的對象數(shù)據(jù)失效
this.remPartion = function(filename){
me.remAtrib(filename,"expire");
return true;
}
//設(shè)置有效期
this.setExpire = function(sec){
var oTimeNow = new Date();
oTimeNow.setSeconds(oTimeNow.getSeconds() + parseInt(sec));
return oTimeNow.getTime();
}
}
}
其中CMInfo類見 一些常用的JS功能函數(shù)(一) (2009-06-04更新)
需要說明的是因?yàn)檫€沒用到實(shí)際項(xiàng)目中,因此還不知其兼容性和穩(wěn)定性如何,如果網(wǎng)友發(fā)現(xiàn)了BUG,還望指出。謝謝
這個也是html5中提出來,在瀏覽器關(guān)閉以后,使用globalStorage存儲的信息仍能夠保留下來,并且存儲容量比IE的userdata大得多,一個域下面是5120k。和sessionStorage一樣,域中任何一個頁面存儲的信息都能被所有的頁面共享。
作用域
globalStorage['z.baidu.com'] 所有z.baidu.com下面的頁面都可以使用這塊空間
globalStorage['baidu.com'] 所有baidu.com下面的頁面都可以使用這塊空間
globalStorage['com']:所有com域名都可以 共享的使用這一塊空間
globalStorage[''] :所有頁面都可以使用的空間
現(xiàn)在Firefox只支持當(dāng)前域下的globalStorage存儲, 如果使用公用域會導(dǎo)致一個這樣一個類似的錯誤“Security error” code: “1000”。
過期時間
按照HTML5的描述,globalStorage只在安全問題或者當(dāng)用戶要求時才會過期,瀏覽器應(yīng)該避免刪除那些正在被腳本訪問的數(shù)據(jù),并且userdata應(yīng)該是用戶可寫的。
因此我們的腳本要能夠控制過期時間,可以在globalStorage的某個區(qū)域存儲過期時間,在load的時候判斷是否過期,可以在一定程度上解決過期時間的問題。
存儲時,同時存儲過期時間
以上是我從網(wǎng)上查詢到的資料,為了兼容非IE瀏覽器“userdata”,我改進(jìn)了之前我自己寫的一個
“userdata”(見 UserData使用總結(jié)) ,現(xiàn)在是兼容IE和支持globalStorage的瀏覽器了。
復(fù)制代碼 代碼如下:
function behaviorUserdata(udObj)
{
var me = this;
if(CMInfo.Bs_Name=='IE') //IE下用userdata實(shí)現(xiàn)客戶端存儲
{
var loaded = ''; //當(dāng)前已載入的文件名
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;}
}
//預(yù)加載
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); //將對象數(shù)據(jù)保存到名為filename的XML文件里面
return true;
}
//設(shè)置指定的屬性值
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);
}
//設(shè)置一個系列的對象數(shù)據(jù)(即整個XML文件)失效
this.remPartion = function(filename){
if(me.exist(filename))
{
me.udObj.expires = me.setExpire(-1);
me.udObj.save(filename);
}
}
//設(shè)置有效期
this.setExpire = function(sec){
var oTimeNow = new Date();
oTimeNow.setSeconds(oTimeNow.getSeconds() + parseInt(sec));
return oTimeNow.toUTCString();
}
}else //非IE下用globalStorage實(shí)現(xiàn)客戶端存儲
{
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;
}
//設(shè)置指定的屬性值
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;
}
//設(shè)置一個系列的對象數(shù)據(jù)失效
this.remPartion = function(filename){
me.remAtrib(filename,"expire");
return true;
}
//設(shè)置有效期
this.setExpire = function(sec){
var oTimeNow = new Date();
oTimeNow.setSeconds(oTimeNow.getSeconds() + parseInt(sec));
return oTimeNow.getTime();
}
}
}
其中CMInfo類見 一些常用的JS功能函數(shù)(一) (2009-06-04更新)
需要說明的是因?yàn)檫€沒用到實(shí)際項(xiàng)目中,因此還不知其兼容性和穩(wěn)定性如何,如果網(wǎng)友發(fā)現(xiàn)了BUG,還望指出。謝謝
相關(guān)文章
javascript+HTML5的canvas實(shí)現(xiàn)七夕情人節(jié)3D玫瑰花效果代碼
這篇文章主要介紹了javascript+HTML5的canvas實(shí)現(xiàn)七夕情人節(jié)3D玫瑰花效果代碼,使用了html5的canvas技術(shù),可呈現(xiàn)出帶有3D效果的玫瑰花漸顯效果,非常逼真自然,需要的朋友可以參考下2015-08-08原生js實(shí)現(xiàn)簡單的Ripple按鈕實(shí)例代碼
本篇文章主要介紹了原生js實(shí)現(xiàn)簡單的Ripple按鈕實(shí)例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03如何讓你的JavaScript函數(shù)更加優(yōu)雅詳解
在Js世界中有些操作會讓你無法理解,但是卻無比優(yōu)雅,下面這篇文章主要給大家介紹了關(guān)于如何讓你的JavaScript函數(shù)更加優(yōu)雅的相關(guān)資料,需要的朋友可以參考下2021-07-07在Z-Blog中運(yùn)行代碼[html][/html](純JS版)
在Z-Blog中運(yùn)行代碼[html][/html](純JS版)...2007-03-03JavaScript下一版本標(biāo)準(zhǔn)ES6的Set集合使用詳解
ES6:全稱ECMAScript 6.0,是JavaScript語言的國際標(biāo)準(zhǔn),JavaScript是ECMAScript的實(shí)現(xiàn)。今天我們就來學(xué)習(xí)一下ES6的Set集合的使用2023-02-02