使用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创建

  1. o=document.createElement(‘input’);
  2. o.type=’hidden’;
  3. o.addBehavior(‘#default#userData’);
  4. //等同于 o.style.behavior=”url(‘#default#userData’)”;
  5. 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.封装

  1. typeof window.localStorage == 'undefined' && ~function(){
  2.  
  3. var localStorage = window.localStorage = {},
  4.     prefix = 'data-userdata',
  5.     doc = document,
  6.     attrSrc = doc.body,
  7.     html = doc.documentElement,
  8.  
  9.     // save attributeNames to <html>'s
  10.     // data-userdata attribute
  11.     mark = function(key, isRemove, temp, reg){
  12.  
  13.         html.load(prefix);
  14.         temp = html.getAttribute(prefix);
  15.         reg = RegExp('\\b' + key + '\\b,?', 'i');
  16.  
  17.         hasKey = reg.test(temp) ? 1 : 0;
  18.  
  19.         temp = isRemove ? temp.replace(reg, '').replace(',', '') :
  20.                 hasKey ? temp : temp === '' ? key :
  21.                     temp.split(',').concat(key).join(',');
  22.  
  23.  
  24.         html.setAttribute(prefix, temp);
  25.         html.save(prefix);
  26.  
  27.     };
  28.  
  29. // add IE behavior support
  30. attrSrc.addBehavior('#default#userData');
  31. html.addBehavior('#default#userData');
  32.  
  33. //
  34. localStorage.getItem = function(key){
  35.     attrSrc.load(key);
  36.     return attrSrc.getAttribute(key);
  37. };
  38.  
  39. localStorage.setItem = function(key, value){
  40.     attrSrc.setAttribute(key, value);
  41.     attrSrc.save(key);
  42.     mark(key);
  43. };
  44.  
  45. localStorage.removeItem = function(key){
  46.     attrSrc.removeAttribute(key);
  47.     attrSrc.save(key);
  48.     mark(key, 1);
  49. };
  50.  
  51. // clear all attributes on <body> that using for textStorage
  52. // and clearing them from the 'data-userdata' attribute's value of <html>
  53. localStorage.clear = function(){
  54.  
  55.     html.load(prefix);
  56.  
  57.     var attrs = html.getAttribute(prefix).split(','),
  58.         len = attrs.length;
  59.  
  60.     for(var i=0;i<len;i++){
  61.         attrSrc.removeAttribute(attrs[i]);
  62.         attrSrc.save(attrs[i]);
  63.     };
  64.  
  65.     html.setAttribute(prefix,'');
  66.     html.save(prefix);
  67.  
  68. };
  69.  
  70. }();

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就不说了。

原创粉丝点击