localStorage兼容ie6/7 用addBehavior 实现
制作过程我就不说了,程序下面会占出来
define(function(){
if('localStorage' in window) return;
function Storage(){
this.box = document.body || document.getElementByTagName('head')[0] || document.documentElement;
this.name = 'localStorage'
this.data = document.createElement(this.name);
this.data.addBehavior("#default#userData");
this.box.appendChild(this.data);
this.map = [];
this.length = this.length();
}
Storage.prototype.setItem = function(name,val){
if(name=='localStorage-map'){
throw new Error("this is localStorage in key [localStorage-map] not use!")
return ;
}
if(this.map.length==0){
this.data.load('localStorage-map');
var data = this.data.getAttribute('localStorage-map');
if(data!=null){
this.map = data.split(',');
}
}
var flag = true;
for(var i in this.map){
if(this.map[i] == name){
flag = false;
}
}
if(flag){
this.map.push(name)
}
this.data.setAttribute(name,val);
var date = new Date();
date.setDate(date.getDate()+700);
this.data.expires = date.toUTCString();
this.data.save(name);
this.data.setAttribute('localStorage-map',this.map);
this.data.save('localStorage-map');
}
Storage.prototype.getItem = function(name){
if(name == 'localStorage-map'){
throw new Error("this is localStorage in key [localStorage-map] not use!");
return;
}
this.data.load(name);
return this.data.getAttribute(name);
};
Storage.prototype.length = function(){
if(this.map.length==0){
this.data.load('localStorage-map');
var data = this.data.getAttribute('localStorage-map');
if(data!=null){
this.map = data.split(',');
}
}
for (var i = this.map.length - 1; i >= 0; i--) {
alert(this.getItem(this.map[i]))
if(this.getItem(this.map[i])==undefined || this.getItem(this.map[i])==""){
this.map.splice(i,1);
}
}
return this.map.length;
};
Storage.prototype.removeItem = function(name){
if(typeof name=="undefined" || name=="") return;
if(this.map.length==0){
if(this.getItem('localStorage-map')!=null){
this.map = this.getItem('localStorage-map').split(',');
}
}
for(var i in this.map){
if(this.map[i] == name){
this.map.splice(i,1);
}
}
this.data.load(name);
this.data.setAttribute(name,undefined);
this.data.save(name);
return true;
},
Storage.prototype.clear=function(){
if(this.map.length==0){
if(this.getItem('localStorage-map')!=null){
this.map = this.getItem('localStorage-map').split(',');
}
}
for(var i in this.map){
this.removeItem(this.map[i]);
}
}
window.localStorage = new Storage();
});
调用程序
localStorage.setItem('value','{askdjf:ddd}')
localStorage.setItem('name','{ddddddd:ddd}')
alert(localStorage.getItem('value'))
alert(localStorage.getItem('name'))
alert(localStorage.removeItem('name'))
alert(localStorage.length);
我只是简单测试了一下可以,不知道哪里还有问题,望指点一二!!!!!
localStorage兼容ie6/7 用addBehavior 实现的更多相关文章
- localStorage兼容方案
localStorage是H5的存储方案,各大浏览器支持都相当不错,唯一悲催的就是IE,这个浏览器界的另类总是显得格格不入. IE “Internet选项”->“安全”中有一个“启动保护模式”的 ...
- JavaScript简单分页,兼容IE6,~3KB
简介 兼容IE6+及现代浏览器的简单分页,支持同一页面多个分页. 使用 Browser <link rel="stylesheet" href="css/GB-pa ...
- html5 图片上传,支持图片预览、压缩、及进度显示,兼容IE6+及标准浏览器
以前写过上传组件,见 打造 html5 文件上传组件,实现进度显示及拖拽上传,兼容IE6+及其它标准浏览器,对付一般的上传没有问题,不过如果是上传图片,且需要预览的话,就力有不逮了,趁着闲暇时间,给上 ...
- JQuery兼容IE6问题汇总(不断更新)
兼容IE6真是苦逼的差事,无奈中... 逗号的问题:IE6中要去掉最后的逗号 var o={ id:1, Name:"abc", //这里的逗号一定要去掉 } HTML的结构,由于 ...
- .使用 HTML+CSS 实现如图布局,border-widht 5px,一个格子大小是 50*50,hover时候边框变为红色(兼容IE6+,考虑语义化的结构)
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- 表单美化-原生javascript和jQuery多选按钮(兼容IE6)
前些天我们讲了下单选按钮的美化今天来做表单元素多选按钮的美化.我们的想法是:利用多选按钮是否被选中和是否不给选择的特性来为按钮的父元素添加对应的样式,就是说用什么的样式是由按钮的状态来决定. 用到的图 ...
- min-height最小高度的实现(兼容IE6、IE7、FF)(解决IE6不兼容min-height)
<!doctype html><html> <head> <meta charset="UTF-8"> <meta name= ...
- 一款兼容IE6并带有多图横向滚动的jquery特效
一款兼容IE6并带有多图横向滚动的jquery特效,自动切换多个图片的jquery特效效果, 为大家分享这个的原因是,这款特效在兼容IE6上面很完美,实用性就广很多了. 适用浏览器:IE6.IE7.I ...
- jQuery编写的一款兼容IE6的图片轮播幻灯片
jQuery编写的一款兼容IE6的图片轮播幻灯片,很不错的一款jquery特效.大家可以下载下来研究研究. 每隔几秒就自动切换一波图片,此效果兼容性还做的不错,适合居多的浏览器. 适用浏览器:IE6. ...
随机推荐
- null 和 undefined 的区别
null表示"没有对象",即该处不应该有值. (1) 作为函数的参数,表示该函数的参数不是对象. (2) 作为对象原型链的终点. undefiend 就是一个缺少值,此处应该有的值 ...
- java.util.concurrent包分类结构图
摘自:http://blog.csdn.net/tsyj810883979/article/details/6956290
- json 递归查找某个节点
一段json可能有很多的子节点,需要查询到某一个节点 用到的js是 find-in-json.js 地址是:https://gist.github.com/iwek/3924925 貌似翻|||墙才能 ...
- HBase Cassandra Riak HyperTable
Cassandra HBase 一致性 Quorum NRW策略 通过Goss ...
- HTTP-Session工作机制
HTTP-Session将HTTP这种无状态协议通过session来保存状态.然而session通过记录服务器生成编号来标识协议,从而服务器就可以识别协议的状态. session保存服务器端, co ...
- HDU 3308 LCIS
题意: U A B: 把第A个数变成BQ A B: 输出[A,B]最长连续上升子序列(注意是连续 相当于子串) 思路:单点更新 ,区间合并几下左边开头最小 和右边结束最大的两个数即可. #incl ...
- php max_input_vars限制数组大小
今天做一个项目需要post2000个数组过去,发现一直只能接到一半,后来发现是max_input_vars显示问题. 修改php.ini里面max_input_vars的大小就可以了
- py 抓取中文网址
- SharePoint 2016 开发 工具Preview发布
博客地址:http://blog.csdn.net/FoxDave 之前装了SharePoint,但是并不能在Visual Studio 2015里面做开发,因为没有相应的office tool. 但 ...
- Python学习路程day8
Socket语法及相关 socket概念 A network socket is an endpoint of a connection across a computer network. Toda ...