跨瀏覽器的本機存放區解決方案

來源:互聯網
上載者:User

跨瀏覽器的本機存放區多種方式,例如:
1、localStorage:只支援IE8+、FireFox、Chrome、Opera等,不支援IE8以下的瀏覽器。
2、瀏覽器Cookie:支援的資料存放區量相對較少,每個domain最多隻能有20條cookie,每個cookie長度不能超過4KB,否則會被截掉,有些瀏覽器甚至不支援;同時,Cookie存在安全性問題,如果cookie被人攔截了,就可以取得所有的session資訊。
3、可以在頁面上嵌一個隱藏的Flash,然後使用Flash的Flash SharedObject,它基本上不會有相容性問題,只有要額外的引入Flash和JS,但這樣會增加頁面負擔。
4、User Data: 是微軟為IE專門在系統中開闢的一Block Storage空間(這個支援所有IE瀏覽器),只支援Windows+IE的組合(單個檔案的大小限制是128KB,一個網域名稱下總共可以儲存1024KB的檔案,檔案個數應該沒有限制。在受限網站裡這兩個值分別是64KB和640KB。)
綜上,我們可以用以localStorage+User Data結合的方式來作為本機存放區的解決方案:

(function (window) {    window.localDataStore = {        hname: location.hostname ? location.hostname : 'localStatus',        isLocalStorage: window.localStorage ? true : false,        dataDom: null,        initDom: function () {            if (!this.dataDom) {                try {                    this.dataDom = document.createElement('input');                     this.dataDom.type = 'hidden';                    this.dataDom.style.display = "none";                    this.dataDom.addBehavior('#default#userData');                     document.body.appendChild(this.dataDom);                    var exDate = new Date();                    exDate = exDate.getDate() + 30;                    this.dataDom.expires = exDate.toUTCString();                 } catch (ex) {                    return false;                }            }            return true;        },        set: function (key, value) {            if (this.isLocalStorage) {                window.localStorage.setItem(key, value);            } else {                if (this.initDom()) {                    this.dataDom.load(this.hname);                    this.dataDom.setAttribute(key, value);                    this.dataDom.save(this.hname)                }            }        },        get: function (key) {            if (this.isLocalStorage) {                return window.localStorage.getItem(key);            } else {                if (this.initDom()) {                    this.dataDom.load(this.hname);                    return this.dataDom.getAttribute(key);                }            }        },        remove: function (key) {            if (this.isLocalStorage) {                localStorage.removeItem(key);            } else {                if (this.initDom()) {                    this.dataDom.load(this.hname);                    this.dataDom.removeAttribute(key);                    this.dataDom.save(this.hname)                }            }        }    }})(window);

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.