javascript globalStorage類代碼
更新時(shí)間:2009年06月04日 17:48:38 作者:
非IE瀏覽器“userdata”的解決方案
globalStorage
這個(gè)也是html5中提出來,在瀏覽器關(guān)閉以后,使用globalStorage存儲(chǔ)的信息仍能夠保留下來,并且存儲(chǔ)容量比IE的userdata大得多,一個(gè)域下面是5120k。和sessionStorage一樣,域中任何一個(gè)頁面存儲(chǔ)的信息都能被所有的頁面共享。
作用域
globalStorage['z.baidu.com'] 所有z.baidu.com下面的頁面都可以使用這塊空間
globalStorage['baidu.com'] 所有baidu.com下面的頁面都可以使用這塊空間
globalStorage['com']:所有com域名都可以 共享的使用這一塊空間
globalStorage[''] :所有頁面都可以使用的空間
現(xiàn)在Firefox只支持當(dāng)前域下的globalStorage存儲(chǔ), 如果使用公用域會(huì)導(dǎo)致一個(gè)這樣一個(gè)類似的錯(cuò)誤“Security error” code: “1000”。
過期時(shí)間
按照HTML5的描述,globalStorage只在安全問題或者當(dāng)用戶要求時(shí)才會(huì)過期,瀏覽器應(yīng)該避免刪除那些正在被腳本訪問的數(shù)據(jù),并且userdata應(yīng)該是用戶可寫的。
因此我們的腳本要能夠控制過期時(shí)間,可以在globalStorage的某個(gè)區(qū)域存儲(chǔ)過期時(shí)間,在load的時(shí)候判斷是否過期,可以在一定程度上解決過期時(shí)間的問題。
存儲(chǔ)時(shí),同時(shí)存儲(chǔ)過期時(shí)間
以上是我從網(wǎng)上查詢到的資料,為了兼容非IE瀏覽器“userdata”,我改進(jìn)了之前我自己寫的一個(gè)
“userdata”(見 UserData使用總結(jié)) ,現(xiàn)在是兼容IE和支持globalStorage的瀏覽器了。
function behaviorUserdata(udObj)
{
var me = this;
if(CMInfo.Bs_Name=='IE') //IE下用userdata實(shí)現(xiàn)客戶端存儲(chǔ)
{
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 "";
}
//移除對(duì)象的指定屬性
this.remAtrib = function(filename,atrib){
me.udObj.removeAttribute(atrib);
me.udObj.save(filename); //將對(duì)象數(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è)置一個(gè)系列的對(duì)象數(shù)據(jù)(即整個(gè)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)客戶端存儲(chǔ)
{
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];
}
//移除對(duì)象的指定屬性
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è)置一個(gè)系列的對(duì)象數(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,還望指出。謝謝
這個(gè)也是html5中提出來,在瀏覽器關(guān)閉以后,使用globalStorage存儲(chǔ)的信息仍能夠保留下來,并且存儲(chǔ)容量比IE的userdata大得多,一個(gè)域下面是5120k。和sessionStorage一樣,域中任何一個(gè)頁面存儲(chǔ)的信息都能被所有的頁面共享。
作用域
globalStorage['z.baidu.com'] 所有z.baidu.com下面的頁面都可以使用這塊空間
globalStorage['baidu.com'] 所有baidu.com下面的頁面都可以使用這塊空間
globalStorage['com']:所有com域名都可以 共享的使用這一塊空間
globalStorage[''] :所有頁面都可以使用的空間
現(xiàn)在Firefox只支持當(dāng)前域下的globalStorage存儲(chǔ), 如果使用公用域會(huì)導(dǎo)致一個(gè)這樣一個(gè)類似的錯(cuò)誤“Security error” code: “1000”。
過期時(shí)間
按照HTML5的描述,globalStorage只在安全問題或者當(dāng)用戶要求時(shí)才會(huì)過期,瀏覽器應(yīng)該避免刪除那些正在被腳本訪問的數(shù)據(jù),并且userdata應(yīng)該是用戶可寫的。
因此我們的腳本要能夠控制過期時(shí)間,可以在globalStorage的某個(gè)區(qū)域存儲(chǔ)過期時(shí)間,在load的時(shí)候判斷是否過期,可以在一定程度上解決過期時(shí)間的問題。
存儲(chǔ)時(shí),同時(shí)存儲(chǔ)過期時(shí)間
以上是我從網(wǎng)上查詢到的資料,為了兼容非IE瀏覽器“userdata”,我改進(jìn)了之前我自己寫的一個(gè)
“userdata”(見 UserData使用總結(jié)) ,現(xiàn)在是兼容IE和支持globalStorage的瀏覽器了。
復(fù)制代碼 代碼如下:
function behaviorUserdata(udObj)
{
var me = this;
if(CMInfo.Bs_Name=='IE') //IE下用userdata實(shí)現(xiàn)客戶端存儲(chǔ)
{
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 "";
}
//移除對(duì)象的指定屬性
this.remAtrib = function(filename,atrib){
me.udObj.removeAttribute(atrib);
me.udObj.save(filename); //將對(duì)象數(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è)置一個(gè)系列的對(duì)象數(shù)據(jù)(即整個(gè)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)客戶端存儲(chǔ)
{
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];
}
//移除對(duì)象的指定屬性
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è)置一個(gè)系列的對(duì)象數(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)文章
js實(shí)現(xiàn)字符串和數(shù)組之間相互轉(zhuǎn)換操作
這篇文章主要介紹了js實(shí)現(xiàn)字符串和數(shù)組之間相互轉(zhuǎn)換操作的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-01-01
Electron?自定義窗口桌面時(shí)鐘實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了Electron?自定義窗口桌面時(shí)鐘實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
JavaScript 基礎(chǔ)表單驗(yàn)證示例(純Js實(shí)現(xiàn))
下面小編就為大家?guī)硪黄狫avaScript 基礎(chǔ)表單驗(yàn)證示例(純Js實(shí)現(xiàn))。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
javascript勻速動(dòng)畫和緩沖動(dòng)畫詳解
這篇文章主要為大家詳細(xì)介紹了javascript勻速動(dòng)畫和緩沖動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
詳解JavaScript實(shí)現(xiàn)動(dòng)態(tài)的輪播圖效果
這篇文章主要介紹了JavaScript實(shí)現(xiàn)動(dòng)態(tài)的輪播圖效果,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
jQuery EasyUI window窗口使用實(shí)例代碼
本文通過實(shí)例代碼給大家介紹了jQuery EasyUI window窗口使用功能,代碼簡單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-12-12

