由於HTTP是無狀態的,你是誰?你乾了什麼?抱歉伺服器都是不知道的。
因此session(會話)出現了,它會在伺服器上儲存使用者資訊以便將來使用(例如使用者名稱,購物車購買商品等)。
但是session是暫時的,使用者離開網站將被刪除。如果要永久存儲信息,可以保存在資料庫中!
session工作原理:為每個使用者建立一個session id(核心!!!)。而session id是儲存在cookie中的,也就是說如果瀏覽器停用了cookie,那麼session會失效! (但可以透過其它方式實現,如:透過URL傳遞session id)
使用者驗證一般採用session。
cookie:目的:網站標記使用者身分而儲存在本機用戶端的資料(通常經過加密)。
cookie會在同源的http請求攜帶(即使不需要),即在客戶端和伺服器之間來回傳遞!
cookie的資料大小不超過4k
cookie的有效期限:設定的cookie有效時間之前一直有效,即使瀏覽器關閉!
localStorage & sessionStorage:早期,本地快取普遍使用的是cookie,但是web儲存需要更安全、更快速!
這些資料不會保存在伺服器上(儲存在客戶端),不會影響伺服器效能!
sessionStorage和localStorage資料儲存也有大小限制,但卻比cookie大得多,可以達到5M甚至更大!
localStorage:沒有時間限制的資料儲存!
sessionStorage:由英文意思也可知,它是對session的資料存儲,所以在使用者關閉瀏覽器(標籤頁/視窗)後,資料被刪除!
HTML5 web儲存支援情況:IE8以上,現代瀏覽器。
資料以鍵值對儲存:
localStorage和sessionStorage都有以下幾種方法:
<!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>
程式運行結果:
注意:鍵值對是以字串保存的,根據需求應改變類型(例如做加法,變成Number型)。
HTML5運用程式快取(Application Cache):透過建立cache manifest文件,web運用可被緩存,且無網路狀態可以進行存取!
Application Cache優勢:1.離線瀏覽;
2.速度更快:已快取資源載入更快;
3.減少瀏覽器負載:客戶端將只從伺服器下載或更新已變更的資源
支持情況:
IE10以上,現代瀏覽器。
使用:
<!DOCTYPE html><html manifest=demo.appcache></html>
注意:要開啟application cache,需指定manifest屬性(副檔名:.appcache);如果未指定manifest屬性,頁面不會快取(除非在manifest檔中直接指定了該頁!)
manifest檔案在伺服器上需要正確的設定MIME-type:text/cache-manifest。
Manifest文件:manifest是簡單的文字文件,它告知瀏覽器被快取的內容以及不被快取的內容!
manifest可分為三個部分:CACHE MANIFEST:此項目列出的檔案將在首次下載後進行快取!
NETWORK:此項目列出的檔案需要與伺服器進行網路連接,且不會被快取!
FALLBACK:此項目列出當頁面無法存取時的回退頁面(如:404頁面)!
test.appcache:
CACHE MANIFEST#2017 11 21 v10.0.1/test.css/logo.gif/main.jsNETWORK/login.php/register.phpFALLBACK#/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頁面上執行腳本時,除非腳本載入完成,否則頁面不會回應!
支援情況:IE10以上,現代瀏覽器
範例: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>計數:<output id=count></output></p> <button onclick=startWorker()>開始</button> <button onclick=overWorker()>結束</button> <script> var w; function startWorker(){ / / 偵測瀏覽器是否支援web worker if(typeof(Worker)!=='undefined'){ if(typeof(w)=='undefined'){ //建立web worker物件w=new Worker('testWorker.js'); } // 事件持續監聽(即使外部腳本已經完成),除非被終止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頁面傳回一段訊息postMessage(i); setTimeout('timedCount()',500);}timedCount();
注意1:通常web worker不是用於如此簡單的任務,而是用在更耗CPU資源的任務!
注意2:在chrome中運行會產生cannot be accessed from origin 'null'的錯誤,我的解決方法是:xampp中開啟apache,用http://localhost/進行訪問。
web worker缺點:由於web worker位於外部文件中,所以它無法存取下列javascript物件:
server-sent事件是單向資訊傳遞;網頁可以自動取得來自伺服器的更新!
以前:網頁先詢問是否有可用的更新,伺服器發送數據,進行更新(雙向數據傳遞)!
支援情況:除IE以外的現代瀏覽器均支援!
範例程式碼: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){ // 建立EventSource物件var source=new EventSource(test .php); // 事件監聽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:
<?phpheader('Content-Type:text/event-stream');header('Cache-Control:no-cache');$time=date('r');echo data:The server time is: {$ time} /n/n;// 刷新輸出資料flush();
注意:後面沒有內容,php檔案可以不用?>關閉!
HTML5 WebSocket:WebSocket可以用在多個標籤頁之間的通訊!
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。