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