使用userdata和localstorage做跨浏览器本地储存
来源:互联网 发布:学python需要什么基础 编辑:程序博客网 时间:2024/06/06 03:23
From :
http://www.dewen.org/q/4108/h5%E7%9A%84localStorage%E5%92%8C%E6%9C%AC%E5%9C%B0%E5%AD%98%E5%82%A8IE6%E3%80%81IE7%E4%B8%8D%E6%94%AF%E6%8C%81
1.浏览器支持
userData是微软为IE在系统中开辟的存储空间。因此只支持windows+IE。意外的是从IE5.5就已经开始userData了。
2.保存位置
在XP下,一般位于C:\Documents and Settings\用户名\UserData,有些时候会在C:\Documents and Settings\用户名\Application Data\Microsoft\Internet Explorer\UserData。
在Vista下,位于C:\Users\用户名\AppData\Roaming\Microsoft\Internet Explorer\UserData。
userData的保存形式为XML文件。
3.大小限制
Security Zone Document Limit (KB) Domain Limit (KB)
Local Machine 128 1024
Intranet 512 10240
Trusted Sites 128 1024
Internet 128 1024
Restricted 64 640
线上使用时,单个文件大小限制为128KB,一个域名下文件大小限制为1024KB,文件数应该没有限制。在受限站点里这两个值分别是64KB和640KB,所以如果考虑到各种情况的话,单个文件最好能控制64KB以下。
4.使用
userData可以绑定到,几乎所有标签上。
官方文档还加了说明:
Setting the userData behavior class on the html, head, title, or style object causes an error when the save or load method is called.
apply to:
A, ABBR, ACRONYM, ADDRESS, AREA, B, BIG, BLOCKQUOTE, BUTTON, CAPTION, CENTER, CITE, CODE, DD, DEL, DFN, DIR, DIV, DL, DT, EM, FONT, FORM, hn, HR, I, IMG, INPUT type=button, INPUT type=checkbox, INPUT type=file, INPUT type=hidden, INPUT type=image, INPUT type=password, INPUT type=radio, INPUT type=reset, INPUT type=submit, INPUT type=text, KBD, LABEL, LI, LISTING, MAP, MARQUEE, MENU, OBJECT, OL, OPTION, P, PLAINTEXT, PRE, Q, S, SAMP, SELECT, SMALL, SPAN, STRIKE, STRONG, SUB, SUP, TABLE, TEXTAREA, TT, U, UL, VAR, XMP
可以使用style方式也可以用js来创建支持userData的对象。
html方式
js创建
- o=document.createElement(‘input’);
- o.type=’hidden’;
- o.addBehavior(‘#default#userData’);
- //等同于 o.style.behavior=”url(‘#default#userData’)”;
- document.body.appendChild(o);
userData提供了以下属性和方法
属性
expires 设置或者读取过期时间
XMLDocument 读取文件的XML DOM
方法
getAttribute 读取指定属性的值
load 打开文件
removeAttribute 删除指定的属性
save 保存文件
setAttribute 为指定属性赋值
5.目录限制
localStorage不能跨域访问,而userData更加严格,不能跨目录访问。
如:
http://example.com/path1
只能是http://example.com/path1/example.php目录下网页文件才可访问 。
而http://example.com/path1/path2目录下所有文件是访问不到path1保存的数据的。
cookie通过设定domain可以跨子域访问。
既然这样,为什么不用cookie来做本地储存?
1.容量小,约4KB
2.cookie可能被禁用
3.cookie不够安全
4.每次cookie都被发送到服务端,增加带宽。这和我们做本地储存的目的不符。
4.javascript被禁用的情况
6.封装
- typeof window.localStorage == 'undefined' && ~function(){
-
- var localStorage = window.localStorage = {},
- prefix = 'data-userdata',
- doc = document,
- attrSrc = doc.body,
- html = doc.documentElement,
-
- // save attributeNames to <html>'s
- // data-userdata attribute
- mark = function(key, isRemove, temp, reg){
-
- html.load(prefix);
- temp = html.getAttribute(prefix);
- reg = RegExp('\\b' + key + '\\b,?', 'i');
-
- hasKey = reg.test(temp) ? 1 : 0;
-
- temp = isRemove ? temp.replace(reg, '').replace(',', '') :
- hasKey ? temp : temp === '' ? key :
- temp.split(',').concat(key).join(',');
-
-
- html.setAttribute(prefix, temp);
- html.save(prefix);
-
- };
-
- // add IE behavior support
- attrSrc.addBehavior('#default#userData');
- html.addBehavior('#default#userData');
-
- //
- localStorage.getItem = function(key){
- attrSrc.load(key);
- return attrSrc.getAttribute(key);
- };
-
- localStorage.setItem = function(key, value){
- attrSrc.setAttribute(key, value);
- attrSrc.save(key);
- mark(key);
- };
-
- localStorage.removeItem = function(key){
- attrSrc.removeAttribute(key);
- attrSrc.save(key);
- mark(key, 1);
- };
-
- // clear all attributes on <body> that using for textStorage
- // and clearing them from the 'data-userdata' attribute's value of <html>
- localStorage.clear = function(){
-
- html.load(prefix);
-
- var attrs = html.getAttribute(prefix).split(','),
- len = attrs.length;
-
- for(var i=0;i<len;i++){
- attrSrc.removeAttribute(attrs[i]);
- attrSrc.save(attrs[i]);
- };
-
- html.setAttribute(prefix,'');
- html.save(prefix);
-
- };
-
- }();
7.可用的框架
(1)store.js
store.js是轻量的JS框架。
用 store.set(‘key’,'value’)和store.get(‘key’,'value’)基本满足了需求。如果是以json形式储存和解析 的话,要使用json.js来使IE支持json对象。除了原生的javascript还可以找到jQuery版本的store.js
(2)其他
USTORE.js https://github.com/hugeinc/USTORE.js
Box.js https://github.com/kbjr/Box.js
8.参考
http://news.ycombinator.com/item?id=1468802
http://msdn.microsoft.com/en-us/library/ms531424%28v=VS.85%29.aspx
http://www.cnblogs.com/QLeelulu/archive/2008/03/29/1129322.html
http://sofish.de/1872
至于localStorage就不说了。
- 使用userdata和localstorage做跨浏览器本地储存
- 用userdata和localstorage做跨浏览器本地储存
- 推荐使用userdata和localstorage做跨浏览器本地储存,具体方案如下:
- 用userdata和localstorage做跨浏览器本地储存(转)
- 浏览器本地存储-localStorage && globalStorage || sessionStorage || UserData
- 使用HTML5本地储存localStorage
- 使用 localStorage和userData 的例子
- js本地储存userData实例
- HTML5 localStorage本地储存
- js本地存储解决方案(localStorage与userData)
- js本地存储解决方案(localStorage与userData)
- js本地存储解决方案(localStorage与userData)
- js本地存储解决方案(localStorage与userData)
- js本地存储解决方案(localStorage与userData)
- js本地存储解决方案(localStorage与userData)
- js本地存储解决方案(localStorage与userData)
- IE UserData 本地存储(替代localStorage)
- 本地存储封装-localStorage,sessionStorage,userData
- JS 获取 URL 中文/英文参数,兼容 IE/Firefox
- 十分钟使用Wamp Server+Wordpress+花生壳动态域名DIY自己的博客
- 遇到html标签的问题
- Blackberry:GSM网络下发送短信
- Directx发展史
- 使用userdata和localstorage做跨浏览器本地储存
- 自己写远程控制软件之总览
- Blackberry:发送邮件
- 工厂模式
- ireport使用教程
- MKS Integrity中如何根据task号来取出change package
- BIT1030 Knight Moves
- Python笔记(6)time模块,random模块
- imx53 时钟dpll分析