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. ...
随机推荐
- jquery的colorbox关闭并传递数据到父窗
function closebox(para1, para2) { var k = parent;// 父窗口对象 k.document.getElementById("para1" ...
- label for
一般我们在使用radio 时都是结合label来使用的 <label><input type=”radio” name=”radio” value=’0’/>男</l ...
- ReferentialConstraint 中的依赖属性映射到由存储生成的列
ReferentialConstraint 中的依赖属性映射到由存储生成的列 这个问题是由于从表中的外键关系建立错误(可能是由于误改),查看从表的所有外键关系,即可找到问题所在. 问题: 什么是从表? ...
- [转]深入理解Java 8 Lambda(类库篇——Streams API,Collectors和并行)
以下内容转自: 作者:Lucida 微博:@peng_gong 豆瓣:@figure9 原文链接:http://zh.lucida.me/blog/java-8-lambdas-insideout-l ...
- 请尝试使用 Console.Read。错误原因
当出现错误: 如果应用程序没有控制台或控制台输入已通过文件进行了重定向,则无法读取键.请尝试使用 Console.Read. 一定是在非控制台应用程序中用了Console.ReadKey(); Con ...
- IT公司100题-28-整数的二进制表示中1的个数
问题描述: 输入一个整数n,求n的二进制表示中,一共有多少个1.例如n=8,二进制表示为00001000,二进制表示中有1个1. 分析: 如果一个数n不为0,那么n-1的二进制表示,与n的二进 ...
- jquery ajax跨域请求webservice webconfig配置
<configuration> <system.web> <compilation debug="true" targetFramework=&quo ...
- android基础(三)ContentProvider
ContentProvider主要用于在不同的应用程序之间实现数据共享,它提供了一套完整的机制,允许一个程序访问另一个程序中的数据,同时还能保证被访问数据的安全性,目前内容提供其实android实现跨 ...
- Unity3D之实现背景的无限重复生成
在制作flappyBird这个小游戏中(摄像机为Orthographic),为了无限重复生成背景,可以先做好三个背景(我做的有点小),在Gamecontroller上挂一个脚本,如下: pu ...
- addslashes() 函数和stripslashes()函数
addslashes() 函数 定义和用法 addslashes() 函数在指定的预定义字符前添加反斜杠. 这些预定义字符是: 单引号 (') 双引号 (") 反斜杠 (\) NULL 语法 ...