惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
GbyAI
GbyAI
MongoDB | Blog
MongoDB | Blog
Jina AI
Jina AI
aimingoo的专栏
aimingoo的专栏
I
InfoQ
IT之家
IT之家
罗磊的独立博客
Blog — PlanetScale
Blog — PlanetScale
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Cloudflare Blog
爱范儿
爱范儿
Microsoft Azure Blog
Microsoft Azure Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
博客园_首页
Engineering at Meta
Engineering at Meta
Martin Fowler
Martin Fowler

博客园 - 匆匆

Grunt常见问题 一个小型的类库 JavaScript 设计模式 HTML5常见问题 改版提示,新手指南 前端开发规范文档 常用正则表达式 JavaScript String 对象扩展方法 JavaScript String 对象常用方法 JavaScript Array 对象扩展方法 JavaScript Array 对象常用方法 JavaScript 常见基础问题二 JavaScript 常见基础问题一 JavaScript与网页性能 页面中添加事件、阻止事件传播、事件删除 常用CSS书写技巧 Gridview用法大总结(牛年珍藏版) 总结一些常用功能源码 JS类库
跨浏览器的本地存储解决方案
匆匆 · 2012-02-07 · via 博客园 - 匆匆

跨浏览器的本地存储解决方案

Posted on 2012-02-07 02:44  匆匆  阅读(3772)  评论()    收藏  举报

跨浏览器的本地存储多种方式,例如:
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专门在系统中开辟的一块存储空间(这个支持所有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);