Web数据存储浅析 Cookie、UserData、SessionStorage、We
Cookie
它是标准的客户端浏览器状态保存方式,可能在浏览器诞生不久就有Cookie了,为什么需要Cookie 这个东东?由于HTTP协议没有状态,所以需要一个标志/存储来记录客户浏览器当前的状态,保证客户浏览器和服务器通讯时可以知道客户浏览器当前的状态。Cookie就是记录这个状态的容器,Cookie在每次请求的时候都被带回到服务器,从而保证了Server可以知道浏览器当前的状态,由于Cookie会被带回到Server,所以Cookie的内容不能存太多,最多不能超过4K,4K 限制的介绍
其中一段内容为
A browser is only required to store up to 300 cookies overall and mata only the last 20 from each doma. The maximum size of a cookie is 4K of disk space.
在一些场景下可能需要存储超过4K或者更多的数据,这些数据不用在每次请求的时候被带回到服务器,只要能在客户的浏览器上保存住,并且可以方便的被Javascript读写就可以了,这种需求尤为在中大型RIA的应用场景下更加的迫切,部分数据放在客户浏览器,节约带宽,提高浏览速度。HTML5标准已经替我们想到了满足这种需求的方案sessionStorage , webSqlDatabase, 微软的IE 有 userData 方案。
userData
微软对USERDATA的介绍:
其中一段内容为
Security AlertFor security reasons, a UserData store is available only the same directory and with the same protocol used to persist the store.
Security AlertUsg this behavior correctly can promise the security of your application. Data a UserData store is not encrypted and therefore not secure. Any application that has aess to the drive where UserData is saved has aess to the data. Therefore, it is remended that you not persist sensitive data like credit card numbers. For more formation, see Security Considerations: DHTML and Default Behaviors.
……
The userData behavior persists data across sessions, usg one UserData store for each object. The UserData store is persisted the cache usg the save and load methods. Once the UserData store has been saved, it can be reloaded even if Microsoft Inter Explorer has been closed and reopened.
Settg the userData behavior class on the html, head, title, or style object causes an error when the save or load method is called.
userData可以在同目录同协议下相互访问,长期存储在客户机器上。最大存储空间也增大了很多。userData需要绑定到一个Dom元素上使用。在userData的method中有removeAttribute方法。经过测试代码发现removeAttribute方法好像不是很管用,需要使用像cookie过期的方式,才可以彻底的删除一个userData Attribute。
在 中介绍说userData存储在X:\Documents and Settgs\当前用户\UserData\ 目录下。具体细节MS在userData说明文档中没有具体说明。
sessionStorage
HTML5 标准对 sessionStorage的介绍
其中对 sessionStorage 的介绍
This specification troduces two related mechanisms, similar to HTTP session cookies [RFC2965], for storg structured data on the client side.
The first is designed for scenarios where the user is carryg out a sgle transaction, but could be carryg out multiple transactions different wdows at the same time.
Cookies dont really handle this case well. For example, a user could be buyg plane tickets two different wdows, usg the same site. If the site used cookies to keep track of which ticket the user was buyg, then as the user clicked from page to page both wdows, the ticket currently beg purchased would "leak" from one wdow to the other, potentially causg the user to buy two tickets for the same flight without really noticg.
To address this, this specification troduces the sessionStorage DOM attribute. Sites can add data to the session storage, and it will be aessible to any page from that orig opened that wdow.
Html5 sessionStorage Demo:
狼蚁网站SEO优化是根据 中提到的IE FF 兼容userData的测试代码
function isIE() {
return !!document.all;
}
function itUserData() {
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 (wdow.sessionStorage) {
//FF 2.0+
try {
sessionStorage.setItem(key, value)
} catch (ex) {
alert(ex);
}
} else {
alert("Error oured user data savg. 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 (wdow.sessionStorage) {
//FF 2.0+
try {
return sessionStorage.getItem(key)
} catch (ex) {
alert(ex)
}
} else {
alert("Error oured user data loadg. 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).toUTCStrg();
save(key);
}
catch (ex) {
alert(ex.message);
}
} else if (wdow.sessionStorage) {
//FF 2.0+
try {
sessionStorage.removeItem(key)
} catch (ex) {
alert(ex)
}
} else {
alert("Error oured user data deletg. your browser do not support user data.")
}
}
userData和sessionStorage共同的特点就是这两个对象都可以存储比cookie大的多的多内容。并且不会随每次请求带回到服务器端。根据Html5标准和测试发现userData和sessionStorage有很多地方是不同的。
狼蚁网站SEO优化是一个测试页面
其中的 SetInsurance lk 会操作javascript 在IE下用userData写数据, 在FF下用sessionStore写数据。在IE下的情况是关闭IE或者重启机器写入的值都不会丢失。在FF下的情况很有意思在本页面写入的值在本页面可以访问,在由本页面所打开的其它页面可以访问。就算本页面开着,在导航栏里输入地址,打开本页面,存入的值就不能访问了。在本页面存入的值,在它的父页面(打开这个页面的页面)是访问不到的。又看了看Html5标准。sessionStorage 的全名是Client-side session and persistent storage of name/value pairs 意思估计是存储在Client的内容是有session 会话的,存储的值由session会话所维系,一旦session会话中断或者丢失,存入的值也就随之消失了。所以当页面没有session(父页面,由地址栏打开的页面),是取不到值的。当FF关闭或者重启机器必然也就取不到值了。
webSqlDatabase
webSqlDatabase在HTML5 标准中是非常Cool的一个东东, 用Javascript写SQL查询,数据库就在浏览器里,这在以前几乎不敢想象。不过今天Safari, Chrome, Opera 都已经支持了,两个webSqlDatabase 的 Demo 页面
W3C 对WEBSQLDATABASE 的介绍页面
WiKi上一个简明的说明
From W3C: "...an API for storg data databases that can be queried usg 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 stead propone Indexed Database API aess.
不知道 HTML 5 的 SQLDB 会被浏览器支持的怎么样, 不过sessionStorage看上去已经可以基本满足需求了。
网站设计
- 静宁会SEO的网站建设公司:全面提升您的网络影
- 提升在线业务的关键:选择最佳的丽水网站建设
- 浙江网站优化发展潜力如何
- 井研专业的网站建设公司:打造您的在线品牌
- 灵山SEO网站建设公司:提升您的在线业务表现
- 蒙城网站建设优化公司:提升您网站表现的理想
- 阳谷企业网站优化:提升线上业务力的关键
- 樟树专业的网站建设公司:打造您在线业务的坚
- 通河百度SEO排名的策略与技巧
- 重庆百度快照排名如何进行精准的客户引流
- 重庆百度快照排名
- 常宁便宜的建站公司:助您轻松打造在线业务
- 巫溪百度网站优化:提升网站曝光率与流量的关
- 湖北整站优化怎么做才能放大客户需求
- 闸北网站建设多少钱?全面解析与预算规划
- 辽宁企业网站优化怎么做电话营销