在介紹HTML5 web緩存前,來認(rèn)識一下cookie和session:
session:
由于HTTP是無狀態(tài)的,你是誰?你干了什么?抱歉服務(wù)器都是不知道的。
因此session(會話)出現(xiàn)了,它會在服務(wù)器上存儲用戶信息以便將來使用(比如用戶名稱,購物車購買商品等)。
但是session是臨時的,用戶離開網(wǎng)站將被刪除。如果要永久存儲信息,可以保存在數(shù)據(jù)庫中!
session工作原理:為每個用戶創(chuàng)建一個session id(核心?。。。?。而session id是存儲在cookie中的,也就是說如果瀏覽器禁用了cookie,那么session會失效?。ǖ强梢酝ㄟ^其它方式實現(xiàn),如:通過URL傳遞session id)
用戶驗證一般采用session。
cookie:
目的:網(wǎng)站標(biāo)記用戶身份而存儲在本地客戶端的數(shù)據(jù)(通常經(jīng)過加密)。
- 用戶訪問網(wǎng)頁時,名字記錄在cookie中;
- 下次繼續(xù)訪問該網(wǎng)頁時,可以從cookie中讀取用戶訪問記錄。
cookie會在同源的http請求攜帶(即使不需要),即在客戶端和服務(wù)器之間來回傳遞!
cookie的數(shù)據(jù)大小不超過4k
cookie的有效期:設(shè)置的cookie有效時間之前一直有效,即使瀏覽器關(guān)閉!
localStorage & sessionStorage:
早期,本地緩存普遍使用的是cookie,但是web存儲需要更安全、更快速!
這些數(shù)據(jù)不會保存在服務(wù)器上(存儲在客戶端),不會影響服務(wù)器性能!
sessionStorage和localStorage數(shù)據(jù)存儲也有大小限制,但卻比cookie大得多,可以達到5M甚至更大!
localStorage:沒有時間限制的數(shù)據(jù)存儲!
sessionStorage:由英文意思也可知,它是對session的數(shù)據(jù)存儲,所以在用戶關(guān)閉瀏覽器(標(biāo)簽頁/窗口)后,數(shù)據(jù)被刪除!
HTML5 web存儲支持情況:
IE8以上,現(xiàn)代瀏覽器。
數(shù)據(jù)以鍵值對存儲:
localStorage和sessionStorage都有以下幾個方法:
- localStorage.setItem(key,value):設(shè)置(保存)數(shù)據(jù);相當(dāng)于localStorage.key=value!
- localStorage.getItem(key):獲取數(shù)據(jù)
- localStorage.removeItem(key):刪除單個數(shù)據(jù)
- localStorage.clear():刪除所有數(shù)據(jù)
- localStorage.key(index):獲取某個索引的鍵值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>web storage</title>
</head>
<body>
<div id="test"></div>
<script>
if (typeof (Storage) != undefined) {
localStorage.name = 'xiao ming';
localStorage.setItem('name1', 'Apple');
document.getElementById('test').innerHTML = "you are: " + localStorage.name;
console.log("first:" + localStorage.name1 + "," + localStorage.key(0));
localStorage.removeItem('name1');
console.log("second: " + localStorage.name1);
console.log("third: " + localStorage.getItem('name'));
localStorage.clear();
console.log("last:" + localStorage.name);
} else {
document.getElementById('test').innerHTML = "更新瀏覽器吧!目前瀏覽器不支持stroage";
}
</script>
</body>
</html>
程序運行結(jié)果:
注意:鍵值對是以字符串保存的,根據(jù)需求應(yīng)改變類型(比如做加法,變?yōu)镹umber型)。
HTML5運用程序緩存(Application Cache):
通過創(chuàng)建cache manifest文件,web運用可被緩存,并且無網(wǎng)絡(luò)狀態(tài)可以進行訪問!
Application Cache優(yōu)勢:
1.離線瀏覽;
2.速度更快:已緩存資源加載更快;
3.減少瀏覽器負(fù)載:客戶端將只從服務(wù)器下載或更新更改過的資源
支持情況:
IE10以上,現(xiàn)代瀏覽器。
使用:
<!DOCTYPE html>
<html manifest="demo.appcache">
</html>
注意:要開啟application cache,需指定manifest屬性(擴展名:.appcache);如果未指定manifest屬性,頁面不會緩存(除非在manifest文件中直接指定了該頁面?。?/p>
manifest文件在服務(wù)器上需正確的配置MIME-type:text/cache-manifest。
Manifest文件:
manifest是簡單的文本文件,它告知瀏覽器被緩存的內(nèi)容以及不被緩存的內(nèi)容!
manifest可分為三部分:
CACHE MANIFEST:此項列出的文件將在首次下載后進行緩存!
NETWORK:此項列出的文件需要與服務(wù)器進行網(wǎng)絡(luò)連接,不會被緩存!
FALLBACK:此項列出當(dāng)頁面無法訪問時的回退頁面(如:404頁面)!
test.appcache:
CACHE MANIFEST
#2017 11 21 v10.0.1
/test.css
/logo.gif
/main.js
NETWORK
/login.php
/register.php
FALLBACK
#/html/目錄中文件無法訪問時,用/offline.html替代
/html/ /offline.html
更新application cache的情況:
1.用戶清空瀏覽器緩存!
2.manifest文件被更改(#:表示注釋,同時如果更改為#2018 1 1 v20.0.0,則瀏覽器會重新緩存!)
3.程序進行更新application cache!
Web Workers:
web workers是運行在后臺的javascript,獨立于其它腳本,不會影響頁面性能!
而一般的HTML頁面上執(zhí)行腳本時,除非腳本加載完成,否則頁面不會響應(yīng)!
支持情況:IE10以上,現(xiàn)代瀏覽器
示例:html文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>web worker</title>
</head>
<body>
<p>計數(shù):<output id="count"></output></p>
<button onclick="startWorker()">開始</button>
<button onclick="overWorker()">結(jié)束</button>
<script>
var w;
function startWorker(){
// 檢測瀏覽器是否支持web worker
if(typeof(Worker)!=='undefined'){
if(typeof(w)=='undefined'){
//創(chuàng)建web worker對象
w=new Worker('testWorker.js');
}
// 事件持續(xù)監(jiān)聽(即使外部腳本已經(jīng)完成),除非被終止
w.onmessage=function(event){
document.getElementById('count').innerHTML=event.data;
};
}else{
document.getElementById('count').innerHTML='瀏覽器不支持web worker';
}
}
function overWorker() {
// 終止web worker對象,釋放瀏覽器/計算機資源
w.terminate();
w=undefined;
}
</script>
</body>
</html>
testWorker.js文件:
var i=0;
function timedCount() {
i+=1;
// 重要的部分,向html頁面?zhèn)骰匾欢涡畔?
postMessage(i);
setTimeout('timedCount()',500);
}
timedCount();
注意1:通常web worker不是用于如此簡單的任務(wù),而是用在更耗CPU資源的任務(wù)!
注意2:在chrome中運行會產(chǎn)生“cannot be accessed from origin 'null'”的錯誤,我的解決方法是:xampp中開啟apache,用http://localhost/進行訪問。
web worker缺點:
由于web worker位于外部文件中,所以它無法訪問下列javascript對象:
- window對象;
- document對象;
- parent對象。
HTML5 server-sent events(服務(wù)器發(fā)送事件):
server-sent事件是單向信息傳遞;網(wǎng)頁可以自動獲取來自服務(wù)器的更新!
以前:網(wǎng)頁先詢問是否有可用的更新,服務(wù)器發(fā)送數(shù)據(jù),進行更新(雙向數(shù)據(jù)傳遞)!
支持情況:除IE以外的現(xiàn)代瀏覽器均支持!
示例代碼:html文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>sever sent event</title>
</head>
<body>
<p>sever sent event informations</p>
<div id="test"></div>
<script>
// 判斷瀏覽器是否支持EventSource
if(typeof(EventSource)!==undefined){
// 創(chuàng)建EventSource對象
var source=new EventSource("test.php");
// 事件監(jiān)聽
source.onmessage=function(event){
document.getElementById('test').innerHTML+=event.data+"<br>";
};
}else{
document.getElementById('test').innerHTML="sorry,瀏覽器不支持server sent event";
}
</script>
</body>
</html>
test.php:
<?php
header('Content-Type:text/event-stream');
header('Cache-Control:no-cache');
$time=date('r');
echo "data:The server time is: {$time} \n\n";
// 刷新輸出數(shù)據(jù)
flush();
注意:后面沒有內(nèi)容,php文件可以不用"?>"關(guān)閉!
HTML5 WebSocket:
- WebSocket是HTML5提供的一種在單個TCP連接上建立全雙工(類似電話)通訊的協(xié)議;
- 瀏覽器和服務(wù)器之間只需要進行一次握手的操作,瀏覽器和服務(wù)器之間就形成了一條快速通道,兩者之間就可直接進行數(shù)據(jù)傳送;
- 瀏覽器通過javascript建立WebSocket連接請求,通過send()向服務(wù)器發(fā)送數(shù)據(jù),onmessage()接收服務(wù)器返回的數(shù)據(jù)。
WebSocket如何兼容低瀏覽器:
- Adobe Flash Socket;
- ActiveX HTMLFile(IE);
- 基于multipart編碼發(fā)送XHR;
- 基于長輪詢的XHR
WebSocket可以用在多個標(biāo)簽頁之間的通信!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。