javascript globalStorage類代碼_javascript技巧

來源:互聯網
上載者:User
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” code: “1000”。
到期時間
按照HTML5的描述,globalStorage只在安全問題或者當使用者要求時才會到期,瀏覽器應該避免刪除那些正在被指令碼訪問的資料,並且userdata應該是使用者可寫的。
因此我們的指令碼要能夠控制到期時間,可以在globalStorage的某個地區儲存到期時間,在load的時候判斷是否到期,可以在一定程度上解決到期時間的問題。
儲存時,同時儲存到期時間
以上是我從網上查詢到的資料,為了相容非IE瀏覽器“userdata”,我改進了之前我自己寫的一個
“userdata”(見 UserData使用總結) ,現在是相容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,還望指出。謝謝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.