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

推荐订阅源

博客园 - 司徒正美
Google Online Security Blog
Google Online Security Blog
博客园_首页
量子位
大猫的无限游戏
大猫的无限游戏
酷 壳 – CoolShell
酷 壳 – CoolShell
The Cloudflare Blog
Vercel News
Vercel News
GbyAI
GbyAI
Y
Y Combinator Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
Cisco Blogs
IT之家
IT之家
A
Arctic Wolf
P
Privacy International News Feed
G
GRAHAM CLULEY
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog
Microsoft Azure Blog
Microsoft Azure Blog
美团技术团队
T
The Blog of Author Tim Ferriss
AWS News Blog
AWS News Blog
A
About on SuperTechFans
P
Proofpoint News Feed
I
Intezer
月光博客
月光博客
Cloudbric
Cloudbric
Google DeepMind News
Google DeepMind News
T
Tor Project blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
www.infosecurity-magazine.com
www.infosecurity-magazine.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Securelist
Engineering at Meta
Engineering at Meta
爱范儿
爱范儿
F
Full Disclosure
V2EX - 技术
V2EX - 技术
Last Week in AI
Last Week in AI
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
人人都是产品经理
人人都是产品经理
C
Check Point Blog
I
InfoQ
S
Security Affairs
Simon Willison's Weblog
Simon Willison's Weblog
P
Palo Alto Networks Blog
F
Fortinet All Blogs
S
Security @ Cisco Blogs
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
K
Kaspersky official blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security

博客园 - 匆匆

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);