今天我們再次回顧一下瀏覽器緩存數據方面的內容:
1.針對IE6以上(含)內核的瀏覽器,我們可以使用UserData 行為(userData Behavior)
說明:
userData 行為通過將數據寫入一個UserData存儲區(UserData store)來保存數據,userData可以將數據以xml格式保存在客戶端計算機上,如果你用的是 Windows 2000 或者 Windows xp,是保存在C:/Documents and Settings/Liming/UserData/文件夾下(如果操作系統不是安裝在C盤,那么C就應該是操作系統所在的分區)。該數據將一直存在,除非你人為刪除或者用腳本設置了該數據的失效期。userData行為提供了一個比Cookie更具有動態性和更大容量的數據結構。每頁的UserData 存儲區數據大小可以達到64 Kb,每個域名可以達到640 Kb。userData 行為通過sessions為每個對象分配UserData存儲區。使用save和load方法將UserData存儲區數據保存在緩存(cache)中。一旦UserData存儲區保存以后,即使IE瀏覽器關閉或者刷新了,下一次進入該頁面,數據也能夠重新載入而不會丟失。出于安全的考慮,相同協議使用同一個文件夾保存UserData存儲區數據。
使用:
(1)首先必須在行內或文檔的head部分聲明如下樣式:
Quote:
.userData {behavior:url(#default#userdata);}
或者使用如下js代碼來聲明:
Quote:
document.documentElement.addBehavior(”#default#userdata”);
(2)成員:
Quote:
expires 設置或取得使用userData行為保存數據的失效日期,使用方法:對象ID.expires = UTC格式的時間字符串;
getAttribute() 取得指定的屬性值;
load(存儲區名) 從userData存儲區載入存儲的對象數據;
removeAttribute() 刪除指定的屬性值;
save(存儲區名) 將對象存儲到一個userData存儲區;
setAttribute() 設置指定的屬性值;
XMLDocument 取得存儲該對象數據的XML DOM引用
(3)示例:
Quote:
document.documentElement.addBehavior("#default#userdata");
document.documentElement.load("t");
document.documentElement.setAttribute("value", "test");
document.documentElement.save("t");
2.針對Firefox2(含)以上內核的瀏覽器,我們可以使用globalStorage
說明:
This is a global object (globalStorage) that maintains multiple PRivate storage areas that can be used to hold data over a long period of time (e.g. over multiple pages and browser sessions).
Specifically, the globalStorage object provides access to a number of different storage objects into which data can be stored. For example, if we were to build a web page that used globalStorage on this domain (developer.mozilla.org) we’d have the following storage object available to us:
globalStorage[’developer.mozilla.org’] - All web pages within the developer.mozilla.org sub-domain can both read and write data to this storage object.
使用:
globalStorage[location.hostname]["t"] = “test”;//賦值
var ret = globalStorage[location.hostname]["t"];//取值
globalStorage.removeItem("t");//刪除
備注:
在firefox2 中,我們可以在tc-eb-test00.tc.baidu.com 上的頁面內使用globalStorage[“baidu.com“][“t“]=“test“來賦值,但是到了firefox3這么做卻會得到一個錯誤提示,原因是firefox3的安全策略要求在使用 globalStorage時,指定的域名和實際的域名必須嚴格匹配!
3.Database Storage,HTML5里的內容,目前僅有Safari支持
說明:
This section is non-normative.
4.針對其它瀏覽器,如常見的Opera,我們可以使用Cookie
說明:
A cookie is a small piece of information stored by the browser. Each cookie is stored in a name=value; pair called a crumb—that is, if the cookie name is “id” and you want to save the id’s value as “this”, the cookie would be saved as id=this. You can store up to 20 name=value pairs in a cookie, and the cookie is always returned as a string of all the cookies that apply to the page. This means that you must parse the string returned to find the values of individual cookies.
Cookies accumulate each time the property is set. If you try to set more than one cookie with a single call to the property, only the first cookie in the list will be retained.
You can use the Microsoft® JScript® split method to extract a value stored in a cookie.
備注:
現代瀏覽器一般默認都支持cookie,但是使用cookie也有幾個致命的弱點:存儲的信息量太少,頁面向服務器發送請求的同時cookie也會被發送,無形中浪費用戶的帶寬。
5.其它解決方案
Google Gear:Google開發出的一種本地存儲技術;
Flash:利用Flash也可以實現本地存儲
這兩種方法的優點是:能支持所有的OS和瀏覽器(Google Gear目前僅支持IE5+和Firefox1.5+);缺點是需要用戶安裝額外的插件,使用起來不如前面其它3種方法簡便。
新聞熱點
疑難解答