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. ...
随机推荐
- 并发编程 04——闭锁CountDownLatch 与 栅栏CyclicBarrier
Java并发编程实践 目录 并发编程 01—— ThreadLocal 并发编程 02—— ConcurrentHashMap 并发编程 03—— 阻塞队列和生产者-消费者模式 并发编程 04—— 闭 ...
- three.js
webgl,three.js交流的论坛,欢迎到里面提问问题 www.webglchina.cn
- 介绍开源的.net通信框架NetworkComms框架之五 心跳检测
原文网址: http://www.cnblogs.com/csdev Networkcomms 是一款C# 语言编写的TCP/UDP通信框架 作者是英国人 以前是收费的 目前作者已经开源 许可是 ...
- 使用Markdown+Pandoc+LaTex+Beamer制作幻灯片
概述 为什么使用markdown? mardown是一种轻量级的标记语言,语法简单,可读性好,并且容易转化成其他格式的文档, 在技术文档撰写中得到越来越广泛的应用.相信大家对markdown都有一定了 ...
- 5、android ConnectivityManager获取网络状态
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ...
- .net 获取AppDomain创建了多少
partial class HttpBrowser { public static string IsolateCall(PageContentHandler pHandler) { Contract ...
- 对编写html代码的几点儿小建议
1.DOCTYPE说明:告诉浏览器要使用哪种规范来解释该文档内容: <!DOCTYPE html PUBLIC "-W3//DTD//XHTML 1.0 Transitional// ...
- html、css、js注释
HTML注释 <!--注释的内容--> CSS注释 /* 注释内容 */ JS注释 单行注释以 // 开头. 多行注释以 /* 开始,以 */ 结尾.
- 线程和NSThread 、 NSOperation
1 使用NSThread实现打地鼠 1.1 问题 NSThread是ios提供的轻量级的多线程解决方案,但是需要自己管理线程的生命周期.线程同步等.本案例使用NSThread实现一个打地鼠的小游戏,在 ...
- tensorflow1
特征: 1 灵活 2 可移植性 3 和研究/生产联系 4 自动求导 5 语言选择:python,严谨的c++接口:未来扩展更多 6 最大性能化