欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Web數(shù)據(jù)存儲淺析 Cookie、UserData、SessionStorage、WebSqlDatabase

  發(fā)布時(shí)間:2010-08-06 00:08:26   作者:佚名   我要評論
淺析Web數(shù)據(jù)存儲-Cookie、UserData、SessionStorage、WebSqlDatabase 的使用方法。

Cookie

它是標(biāo)準(zhǔn)的客戶端瀏覽器狀態(tài)保存方式,可能在瀏覽器誕生不久就有Cookie了,為什么需要Cookie 這個(gè)東東?由于HTTP協(xié)議沒有狀態(tài),所以需要一個(gè)標(biāo)志/存儲來記錄客戶瀏覽器當(dāng)前的狀態(tài),保證客戶瀏覽器和服務(wù)器通訊時(shí)可以知道客戶瀏覽器當(dāng)前的狀態(tài)。Cookie就是記錄這個(gè)狀態(tài)的容器,Cookie在每次請求的時(shí)候都被帶回到服務(wù)器,從而保證了Server可以知道瀏覽器當(dāng)前的狀態(tài),由于Cookie會被帶回到Server,所以Cookie的內(nèi)容不能存太多,最多不能超過4K,4K 限制的介紹 http://ec.europa.eu/ipg/standards/cookies/index_en.htm
其中一段內(nèi)容為:

A browser is only required to store up to 300 cookies overall and maintain only the last 20 from each domain. The maximum size of a cookie is 4K of disk space.

但是在一些場景下可能需要存儲超過4K或者更多的數(shù)據(jù),但是這些數(shù)據(jù)不用在每次請求的時(shí)候被帶回到服務(wù)器,只要能在客戶的瀏覽器上保存住,并且可以方便的被Javascript讀寫就可以了,這種需求尤為在中大型RIA的應(yīng)用場景下更加的迫切,部分?jǐn)?shù)據(jù)放在客戶瀏覽器,節(jié)約帶寬,提高瀏覽速度。HTML5標(biāo)準(zhǔn)已經(jīng)替我們想到了滿足這種需求的方案:sessionStorage , webSqlDatabase, 微軟的IE 有 userData 方案。


userData
微軟對USERDATA的介紹: http://msdn2.microsoft.com/en-us/library/ms531424(VS.85).aspx
其中一段內(nèi)容為:

Security Alert:For security reasons, a UserData store is available only in the same directory and with the same protocol used to persist the store.
Security Alert:Using this behavior incorrectly can compromise the security of your application. Data in a UserData store is not encrypted and therefore not secure. Any application that has access to the drive where UserData is saved has access to the data. Therefore, it is recommended that you not persist sensitive data like credit card numbers. For more information, see Security Considerations: DHTML and Default Behaviors.
……
The userData behavior persists data across sessions, using one UserData store for each object. The UserData store is persisted in the cache using the save and load methods. Once the UserData store has been saved, it can be reloaded even if Microsoft Internet Explorer has been closed and reopened.
Setting the userData behavior class on the html, head, title, or style object causes an error when the save or load method is called.

 

userData可以在同目錄同協(xié)議下相互訪問,長期存儲在客戶機(jī)器上。最大存儲空間也增大了很多。userData需要綁定到一個(gè)Dom元素上使用。在userData的method中有removeAttribute方法。經(jīng)過測試代碼發(fā)現(xiàn)removeAttribute方法好像不是很管用,需要使用像cookie過期的方式,才可以徹底的刪除一個(gè)userData Attribute。
http://www.itwen.com/04web/11skill/skill20060918/60588.html 中介紹說userData存儲在X:\Documents and Settings\當(dāng)前用戶\UserData\ 目錄下。具體細(xì)節(jié)MS在userData說明文檔中沒有具體說明。


sessionStorage
HTML5 標(biāo)準(zhǔn)對 sessionStorage的介紹: http://www.whatwg.org/specs/web-apps/current-work/
其中對 sessionStorage 的介紹:

This specification introduces two related mechanisms, similar to HTTP session cookies [RFC2965], for storing structured data on the client side.
The first is designed for scenarios where the user is carrying out a single transaction, but could be carrying out multiple transactions in different windows at the same time.
Cookies dont really handle this case well. For example, a user could be buying plane tickets in two different windows, using the same site. If the site used cookies to keep track of which ticket the user was buying, then as the user clicked from page to page in both windows, the ticket currently being purchased would "leak" from one window to the other, potentially causing the user to buy two tickets for the same flight without really noticing.
To address this, this specification introduces the sessionStorage DOM attribute. Sites can add data to the session storage, and it will be accessible to any page from that origin opened in that window.

Html5 sessionStorage Demo: http://html5demos.com/storage
下面是根據(jù) http://www.blogjava.net/emu/archive/2006/10/04/73385.html 中提到的IE FF 兼容userData的測試代碼:

復(fù)制代碼
代碼如下:

function isIE() {
return !!document.all;
}
function initUserData() {
if (isIE()) document.documentElement.addBehavior("#default#userdata");
}
function saveUserData(key, value) {
var ex;
if (isIE()) {
//IE
with (document.documentElement) try {
load(key);
setAttribute("value", value);
save(key);
return getAttribute("value");
} catch (ex) {
alert(ex.message)
}
} else if (window.sessionStorage) {
//FF 2.0+
try {
sessionStorage.setItem(key, value)
} catch (ex) {
alert(ex);
}
} else {
alert("Error occured in user data saving. your browser do not support user data.");
}
}
function loadUserData(key) {
var ex;
if (isIE()) {
//IE
with (document.documentElement) try {
load(key);
return getAttribute("value");
} catch (ex) {
alert(ex.message); return null;
}
} else if (window.sessionStorage) {
//FF 2.0+
try {
return sessionStorage.getItem(key)
} catch (ex) {
alert(ex)
}
} else {
alert("Error occured in user data loading. your browser do not support user data.")
}
}
function deleteUserData(key) {
var ex;
if (isIE()) {
//IE
with (document.documentElement) try {
load(key);
expires = new Date(315532799000).toUTCString();
save(key);
}
catch (ex) {
alert(ex.message);
}
} else if (window.sessionStorage) {
//FF 2.0+
try {
sessionStorage.removeItem(key)
} catch (ex) {
alert(ex)
}
} else {
alert("Error occured in user data deleting. your browser do not support user data.")
}
}

userData和sessionStorage共同的特點(diǎn)就是:這兩個(gè)對象都可以存儲比cookie大的多的多內(nèi)容。并且不會隨每次請求帶回到服務(wù)器端。但是根據(jù)Html5標(biāo)準(zhǔn)和測試發(fā)現(xiàn)userData和sessionStorage有很多地方是不同的。

下面是一個(gè)測試頁面:
31_110118_jg9ncookieVsStoragegif 

其中的 SetInsurance link 會操作javascript 在IE下用userData寫數(shù)據(jù), 在FF下用sessionStore寫數(shù)據(jù)。在IE下的情況是:關(guān)閉IE或者重啟機(jī)器寫入的值都不會丟失。在FF下的情況很有意思:在本頁面寫入的值在本頁面可以訪問,在由本頁面所打開的其它頁面可以訪問。但是就算本頁面開著,在導(dǎo)航欄里輸入地址,打開本頁面,存入的值就不能訪問了。在本頁面存入的值,在它的父頁面(打開這個(gè)頁面的頁面)是訪問不到的。又看了看Html5標(biāo)準(zhǔn)。sessionStorage 的全名是:Client-side session and persistent storage of name/value pairs 意思估計(jì)是存儲在Client的內(nèi)容是有session 會話的,存儲的值由session會話所維系,一旦session會話中斷或者丟失,存入的值也就隨之消失了。所以當(dāng)頁面沒有session(父頁面,由地址欄打開的頁面),是取不到值的。當(dāng)FF關(guān)閉或者重啟機(jī)器必然也就取不到值了。


webSqlDatabase
webSqlDatabase在HTML5 標(biāo)準(zhǔn)中是非常Cool的一個(gè)東東, 用Javascript寫SQL查詢,數(shù)據(jù)庫就在瀏覽器里,這在以前幾乎不敢想象。不過今天Safari, Chrome, Opera 都已經(jīng)支持了,兩個(gè)webSqlDatabase 的 Demo 頁面: http://html5demos.com/database http://html5demos.com/database-rollback
W3C 對WEBSQLDATABASE 的介紹頁面: http://dev.w3.org/html5/webdatabase/
WiKi上一個(gè)簡明的說明: http://en.wikipedia.org/wiki/Web_SQL_Database

From W3C: "...an API for storing data in databases that can be queried using a variant of SQL"
Web SQL Database is supported by Google Chrome[1], Opera and Safari but will not be implemented by Mozilla(Firefox)[2] who instead propone Indexed Database API access.

不知道 HTML 5 的 SQLDB 會被瀏覽器支持的怎么樣, 不過sessionStorage看上去已經(jīng)可以基本滿足需求了。

相關(guān)文章

  • 我的css框架——base.css(重設(shè)瀏覽器默認(rèn)樣式)

    有自己css框架在網(wǎng)頁布局過程中會顯得相當(dāng)容易,本文提供的是本人的經(jīng)驗(yàn)之談主要功能在于重設(shè)瀏覽器默認(rèn)樣式,熱愛布局的你可千萬不要錯(cuò)過了哈,希望可以幫助到你
    2013-03-19
  • HTML5本地存儲之Database Storage應(yīng)用介紹

    實(shí)際上,除了sessionStorage和localStorage外,HTML5還支持通過本地?cái)?shù)據(jù)庫進(jìn)行本地?cái)?shù)據(jù)存儲,HTML5采用的是"SQLLite"這種文件型數(shù)據(jù)庫,該數(shù)據(jù)庫多集中在嵌入式設(shè)備上,熟
    2013-01-06
  • HTML5 Web Database 數(shù)據(jù)庫的SQL語句的使用方法

    本文將詳細(xì)介紹HTML5 Web Database 數(shù)據(jù)庫的SQL語句的使用方法,需要了解的朋友可以參考下
    2012-12-09
  • HTML5教程之html 5 本地?cái)?shù)據(jù)庫(Web Sql Database)

    HTML5的Web SQL Databases(html5 本地?cái)?shù)據(jù)庫)的確很唬人,當(dāng)你發(fā)現(xiàn)可以用與mysql查詢一樣的查詢語句來操作本地?cái)?shù)據(jù)庫時(shí),你會發(fā)現(xiàn)這東西挺有趣的,今天,我們一起來了解HT
    2014-04-03
  • ">html 基底網(wǎng)址標(biāo)記

    它的作用就是定一個(gè)全局的樣式。 那你后面的相對路徑會以這個(gè)為基準(zhǔn): <img src="logo.gif" /> 會變成 <img src="http://www.dbjr.com.cn/logo.gif
    2009-04-08
  • 基鏈接標(biāo)簽base的使用介紹

    當(dāng)點(diǎn)了鏈接后,跳出的網(wǎng)頁地址是http://www.webjx.com/web/或http://www.webjx.com/css/,它就是在這些相對路徑的文件前加上基鏈接指向的地址。如果目標(biāo)文件中的鏈接沒有指
    2008-10-17
  • CSS標(biāo)準(zhǔn):vertical-align屬性

    原文:http://www.mikkolee.com/13 最近幾天仔細(xì)研究了一下vertical-align這個(gè)屬性,結(jié)果讓我大吃一驚,這個(gè)很“資深”的CSS標(biāo)準(zhǔn)竟然在各個(gè)瀏覽器里面的表現(xiàn)
    2008-10-17
  • HTML中的base標(biāo)簽使用詳解

    在requireJS中,有一個(gè)屬性叫baseURL,通過設(shè)置baseURL,我們可以將需要加載的文件路徑寫成相對于項(xiàng)目的,而不是相對于當(dāng)前頁面的
    2013-04-24

最新評論