开发过程中涉及本地存储的使用,IE很多版本都不支持localStorage,没办法,就得兼容使用userData了。废话不说了,看代码:

(function(window){
var LS;
(function(){

var o = document.getElementsByTagName("head")[0],
n = window.location.hostname || "localStorage",
d = new Date(),doc,agent;
//typeof o.addBehavior 在IE6下是object,在IE10下是function,因此这里直接用!判断
if(!o.addBehavior)return;//防止有些浏览器默认禁用localStorage,这里优先考虑userData本地存储
try{ //尝试创建iframe代理
agent = new ActiveXObject('htmlfile');
agent.open();
agent.write('<s' + 'cript>document.w=window;</s' + 'cript><iframe src="/favicon.ico"></frame>');
agent.close();
doc = agent.w.frames[0].document;
}catch(e){doc = document;}
o = doc.createElement('head');//这里通过代理document创建head,可以使存储数据垮目录访问
doc.appendChild(o);
d.setDate(d.getDate() + 36500);
o.addBehavior("#default#userData");
o.expires = d.toUTCString();
o.load(n);

var root = o.XMLDocument.documentElement,
attrs = root.attributes,
prefix = "prefix_____hack__",
reg1 = /^[-\d]/,
reg2 = new RegExp("^"+prefix),
encode = function(key){
return reg1.test(key) ? prefix + key : key;
},
decode = function(key){
return key.replace(reg2,"");
};

LS= {
length: attrs.length,
notNativeCode: true,
getItem: function(key){
return (attrs.getNamedItem( encode(key) ) || {nodeValue: null}).nodeValue||root.getAttribute(encode(key)); //IE9中 通过o.getAttribute(name);取不到值,所以才用了下面比较复杂的方法。(也许你会诧异IE9不是有原生的localStorage吗,是的,但是用户可以关闭DOM存储,所以为了保险一些还是考虑IE9可能会使用到#userData吧。)
},
setItem: function(key, value){
root.setAttribute( encode(key), value); //IE9中无法通过 o.setAttribute(name, value); 设置#userData值,而用下面的方法却可以。
o.save(n);
this.length = attrs.length;
},
removeItem: function(key){
root.removeAttribute( encode(key) ); //IE9中无法通过 o.removeAttribute(name); 删除#userData值,而用下面的方法却可以。
o.save(n);
this.length = attrs.length;
},
clear: function(){
while(attrs.length){
this.removeItem( attrs[0].nodeName );
}
this.length = 0;
},
key: function(i){
return attrs[i] ? decode(attrs[i].nodeName) : undefined;
}
};
})();

(function(w,localStorage){//封装LS,对外提供接口
var f = function(){return null;};
w.LS = localStorage?{
set : function(key, value){
//fixed iPhone/iPad 'QUOTA_EXCEEDED_ERR' bug
if( this.get(key) !== undefined )
this.remove(key);
localStorage.setItem(key, value);
},
//查询不存在的key时,有的浏览器返回null,这里统一返回undefined
get : function(key){
var v = localStorage.getItem(key);
return v === null ? undefined : v;
},
remove : function(key){ localStorage.removeItem(key); },
clear : function(){ localStorage.clear(); },
each : function(callback){
var list = this.obj(), fn = callback || function(){}, key;
for(key in list)
if( fn.call(this, key, this.get(key)) === false )
break;
},
obj : function(){
var list={}, i=0, n, key;
if( localStorage.isVirtualObject ){
list = localStorage.key(-1);
}else{
n = localStorage.length;
for(; i<n; i++){
key = localStorage.key(i);
list[key] = this.get(key);
}
}
return list;
}
}:{set:f,get:f,remove:f,clear:f,each:f,obj:f};//如果都不支持则所有方法返回null
})(window,LS||window.localStorage);//这里优先使用自定义的LS
})(window);

使用方法很简单:

对外提供全局变量LS

存储:LS.set('userName',"js明哥哥");

读取:LS.get('userName');

删除:LS.remove('userName');

该组件经过本人测试,暂时没发现什么浏览器不支持,可能测试还不够到位,希望大家多多指点。

使用userData兼容IE6-10,chrome,FF 及360等浏览器的本地存储的更多相关文章

  1. [转载]使用兼容ie6 ie7 ie8 FF的text-overflow:ellips

    使用兼容ie6 ie7 ie8 FF的text-overflow:ellipsis超出文本显示省略号来代替截取函数更有利于seo,如果使用截取函数,源代码中的标题是显示不完整的,即便是在title属性 ...

  2. 浮出层的css写法,完美兼容IE6~10

    利用元素间的绝对定位差一像素,使用不同颜色做出浮出层小三角的效果,完美兼容各浏览器! html部分: <div class="poptip"> <span cla ...

  3. 七行jquery代码实现图片渐变切换【兼容ie6+、 Chrome、 Firefox】

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. 兼容ie6及以上和firefox等标准浏览器的表格行滑过时背景色切换的效果

    一.js代码——"tablehover.js" /**      *②.表格单元行滑过时高亮样式动效组件封装      *oop形式封装交互动效类      *组件说明这个组件是为 ...

  5. Localstorage本地存储兼容函数

    前言HTML5提供了本地存储的API:localstorage对象和sessionStorage对象,实现将数据存储到用户的电脑上.Web存储易于使用.支持大容量(但非无限量)数据同时存储,同时兼容当 ...

  6. 本地存储组件--兼容IE低版本

        在前端开发过程中,会用到本地缓存,但是由于浏览器对不同规范支持的程度不一样,每次进行使用都要为兼容行花费不少时间.我整理了一个本地存储的组件.     组件特点: 可以配置使用localSto ...

  7. div+css 布局下兼容IE6 IE7 FF常见问题

    div+css 布局下兼容IE6 IE7 FF常见问题 收藏 所有浏览器 通用 (市面上主要用到的IE6 IE7 FF)height: 100px; IE6 专用 _height: 100px; IE ...

  8. DIV+CSS IE6/IE7/IE8/FF兼容问题汇总

    1.IE8下兼容问题,这个最好处理,转化成ie7兼容就可以.在头部加如下一段代码,然后只要在IE7下兼容了,IE8下面也就兼容了 <meta http-equiv="x-ua-comp ...

  9. DIV+CSS IE6/IE7/IE8/FF兼容问题大全

    1. [代码][CSS]代码 1, FF下给 div 设置 padding 后会导致 width 和 height 增加, 但IE不会.(可用!important解决) 2, 居中问题. 1).垂直居 ...

随机推荐

  1. SSIS 数据流的执行树和数据管道

    数据流组件的设计愿景是快速处理海量的数据,为了实现该目标,SSIS数据源引擎需要创建执行树和数据管道这两个数据结构,而用户为了快速处理数据流,必须知道各个转换组件的阻塞性,充分利用流式处理流程,利用更 ...

  2. Object C学习笔记5-ARC forbids explicit message* 编译错误

    在学习Object C的过程中XCode 编译器经常出现 "ARC forbids explicit message send of release" 的错误提示. 以上问题主要出 ...

  3. Direct2D处理几何图形之间的碰撞检测(下)

    转载请注明出处:http://www.cnblogs.com/Ray1024 一.概述 上一篇文章中我们介绍了几何图形与点的碰撞检测.几何图形与点的位置关系比较简单:点在几何图形内.点在几何图形外.点 ...

  4. netty+proto使用简要记录

    1. maven环境配置protobuf 2.生成.proto文件 3.将.proto转为java文件 打开电脑的cmd,进入.proto所在文件位置,输入命令:protoc.exe --java_o ...

  5. 简单的Restful工具类

    import java.io.BufferedReader;import java.io.ByteArrayOutputStream;import java.io.Closeable;import j ...

  6. elasticsearch(全文检索)的介绍和安装

    ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开发的,并作为Apach ...

  7. 如何在多机架(rack)配置环境中部署cassandra节点

    cassandra节点上数据的分布和存储是由系统自动完成的.除了我们要设计好partition key之外,在多机架(rack)配置环境中部署cassandra节点,也需要考虑cassandra分布数 ...

  8. 下一代的DevOps服务:AIOps

    AIOps是一个总称,用于指代使用复杂的基础设施管理软件和云解决方案监控工具来实现自动化数据分析和日常的DevOps操作. 那些10年前甚至是5年前构建的系统监控工具的主要缺陷是它们不是为了满足大数据 ...

  9. Hyperledger Fabric(v1.1.0)编译时遇到的问题

    Hyperledger Fabric(v1.1.0)编译时遇到的问题 0. 编译过程的坑 编译时,按照如下顺序编译 make release,编译源码生成二进制文件 make docker,生成一系列 ...

  10. Python3中的函数 大全

    Python 函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.Python提供了许多内建函数,比如print().但也可以自己创建 ...