本地存储在IE下面的编码问题及解决方法
2011-09-03 17:07
127 查看
之前在淘宝实习的时候,做了一个产品,浏览历史记录。评估的时候,主管告诉我要用本地存储来做。本地存储相比cookie的优势就在于,本地存储对于数据的存储时间上没有限制,关闭浏览器以后也不会消失。而本地存储的也有局限:不能跨域,不能跨浏览器,只能存储字符串,而且在IE下面有一些bug。
KISSY.add('localStorage', function(S) {
var doc = document, useObject = doc.documentElement; useObject.style.behavior = 'url(#default#userData)'; // html5 var localStorage = { setItem: function(key, val, context) { return window.localStorage.setItem(key, val, context); }, getItem: function(key, context) { return window.localStorage.getItem(key, context); }, removeItem: function(key, context) { return window.localStorage.removeItem(key, context); }, clear: function() { return window.localStorage.clear(); } }; // Tubie IE 678 only var userBehavor = { setItem: function(key, value, context) { try { useObject.setAttribute(key, value); return useObject.save(context || 'default'); } catch (e) {} }, getItem: function(key, context) { try { useObject.load(context || 'default'); return useObject.getAttribute(key) || ''; } catch (e) {} }, removeItem: function(key, context) { try { context = context || 'default'; useObject.load(context); useObject.removeAttribute(key); return useObject.save(context); } catch (e) {} }, clear: function() { try { useObject.expires = -1; } catch (e) {} } };// return S.merge({// element: useObject// }, (window.localStorage ? localStorage : userBehavor)); S.localStorage = window.localStorage ? localStorage : userBehavor;});
这是之前封装好的代码, 不过这段代码,在IE下面是无法使用clear这个方法的,会报出异常;其次,IE6下面,对“// :”等特殊符号无法解析,也即是说,store方法不能用。这里就需要对字符串进行编码,可以使用unicode或者ASCII编码,我选择了unicode编码,因为可以识别更多的字符嘛。即使将不合法的字符串转换编码后,还是存在问题,因此纯数字的字符串,IE6仍然不识别,我的解决方法是在字符串头加了一个字母。这样,IE6就能试用store方法了。不过,clear方法还是有错。现在只能靠手动删除硬盘文件来实现。。。
KISSY.add('localStorage', function(S) {
var doc = document, useObject = doc.documentElement; useObject.style.behavior = 'url(#default#userData)'; // html5 var localStorage = { setItem: function(key, val, context) { return window.localStorage.setItem(key, val, context); }, getItem: function(key, context) { return window.localStorage.getItem(key, context); }, removeItem: function(key, context) { return window.localStorage.removeItem(key, context); }, clear: function() { return window.localStorage.clear(); } }; // Tubie IE 678 only var userBehavor = { setItem: function(key, value, context) { try { useObject.setAttribute(key, value); return useObject.save(context || 'default'); } catch (e) {} }, getItem: function(key, context) { try { useObject.load(context || 'default'); return useObject.getAttribute(key) || ''; } catch (e) {} }, removeItem: function(key, context) { try { context = context || 'default'; useObject.load(context); useObject.removeAttribute(key); return useObject.save(context); } catch (e) {} }, clear: function() { try { useObject.expires = -1; } catch (e) {} } };// return S.merge({// element: useObject// }, (window.localStorage ? localStorage : userBehavor)); S.localStorage = window.localStorage ? localStorage : userBehavor;});
这是之前封装好的代码, 不过这段代码,在IE下面是无法使用clear这个方法的,会报出异常;其次,IE6下面,对“// :”等特殊符号无法解析,也即是说,store方法不能用。这里就需要对字符串进行编码,可以使用unicode或者ASCII编码,我选择了unicode编码,因为可以识别更多的字符嘛。即使将不合法的字符串转换编码后,还是存在问题,因此纯数字的字符串,IE6仍然不识别,我的解决方法是在字符串头加了一个字母。这样,IE6就能试用store方法了。不过,clear方法还是有错。现在只能靠手动删除硬盘文件来实现。。。
相关文章推荐
- 本地存储在IE下面的编码问题及解决方法
- 【IE信息栏问题】本地html文件js被IE阻止的一些解决方法
- 解决IE不能主动识别UTF-8编码的问题的方法
- dblink远程库表结构修改导致本地库调用此表的存储过程出错问题解决方法
- 【IE信息栏问题】本地html文件js被IE阻止的一些解决方法
- 【IE信息栏问题】本地html文件js被IE阻止的一些解决方法
- python常见编码问题解决方法
- AngularJS在IE下取数据总是缓存问题的解决方法
- 使用php+apc实现上传进度条且在IE7下不显示的问题解决方法
- [乐意黎原创]PHP里由于数组编码导致json_encode输出为空白问题的解决方法
- 【转自loveifa】PHP 获取邮箱未读邮件的方法 根据receivemail改编而来,已解决乱码,编码问题
- ruby 文件中文编码问题 导致passenger start错误的解决方法
- 解决extjs4.1在IE和FF下面字体太上问题,以及按钮的字体是斜体的问题,在extj-all.css后加
- Sybase BCP导入数据时碰到中文编码问题的解决方法
- (转)MySQL5.0中文问题及JDBC数据库连接和JSP汉字编码问题解决方法总结
- 深入Java中文编码乱码问题及最优解决方法
- 本地计算机上的SQL Server(MSSQLSERVER)服务启动后又停止了。问题解决方法之一
- select挡住div的解决方法以及frame问题和IE、FLASH与实用代码
- vs2003 js文件编码问题的解决方法
- 二级菜单ie中被下面的div遮挡解决问题