国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

HTML5 Web 存儲(chǔ)

什麼是 HTML5 Web 儲(chǔ)存?

使用HTML5可以在本機(jī)儲(chǔ)存使用者的瀏覽數(shù)據(jù),是比cookie更好的本機(jī)儲(chǔ)存方式。

早期,本地儲(chǔ)存使用的是cookies。但是Web 儲(chǔ)存需要更加的安全與快速. 這些數(shù)據(jù)不會(huì)被保存在伺服器上,但是這些數(shù)據(jù)只用於用戶請求網(wǎng)站數(shù)據(jù)上.它也可以儲(chǔ)存大量的數(shù)據(jù),而不影響網(wǎng)站的性能.

資料以鍵/值對存在, web網(wǎng)頁的資料只允許該網(wǎng)頁存取使用。


瀏覽器支援

8.jpg

Internet Explorer 8+, Firefox, Opera, Chrome, 和Safari支援Web 儲(chǔ)存。


注意

:?Internet Explorer 7 及更早IE版本不支援web 儲(chǔ)存.

localStorage 和sessionStorage?

  • 有兩個(gè)新物件將資料儲(chǔ)存在客戶端:

    localStorage
  • #- 沒有時(shí)間限制的資料儲(chǔ)存

    sessionStorage
- 針對一個(gè)session 的資料儲(chǔ)存

######在使用web 儲(chǔ)存前,應(yīng)檢查瀏覽器是否支援localStorage 和sessionStorage:###

if(typeof(Storage)!=="undefined")
{
? ?// sessionStorage localStorage 支援!
? ?// 相關(guān)程式碼.....
}
else
{
? ?// 對不起,不支援Web 儲(chǔ)存空間
}


localStorage 物件

localStorage 物件儲(chǔ)存的資料沒有時(shí)間限制。第二天、第二週或下一年之後,數(shù)據(jù)仍然可用。

<!DOCTYPE html>
<html>
<head> 
    <meta charset="UTF-8">
    <title>php中文網(wǎng)(php.cn)</title> 
</head>
<body>
<div id="result"></div>
<script>
    if(typeof(Storage)!=="undefined")
    {
        localStorage.lastname="劉奇";
        document.getElementById("result").innerHTML="姓名: " + localStorage.lastname;
    }
    else
    {
        document.getElementById("result").innerHTML="對不起,您的瀏覽器不支持web存儲(chǔ)……";
    }
</script>
</body>
</html>

執(zhí)行程式嘗試

實(shí)例解析:

#使用key="lastname" 和value="Smith" 建立一個(gè)localStorage 鍵/值對

檢索鍵值為"lastname" 的值然後將資料插入id="result"的元素中

提示:?鍵/值對通常以字串存儲(chǔ),你可以按自己的需求轉(zhuǎn)換該格式。



下面的實(shí)例展示了使用者點(diǎn)擊按鈕的次數(shù).程式碼中的字串值轉(zhuǎn)換為數(shù)字類型:

#
<!DOCTYPE html>
<html>
<head> 
    <meta charset="UTF-8">
    <title>php中文網(wǎng)(php.cn)</title> 
    <script>
        function clickCounter()
        {
            if(typeof(Storage)!=="undefined")
            {
                if (localStorage.clickcount)
                {
                    localStorage.clickcount=Number(localStorage.clickcount)+1;
                }
                else
                {
                    localStorage.clickcount=1;
                }
                document.getElementById("result").innerHTML="點(diǎn)擊按鈕" + localStorage.clickcount + " time(s).";
            }
            else
            {
                document.getElementById("result").innerHTML="對不起,您的瀏覽器不支持web存儲(chǔ)……";            }
        }
    </script>
</head>
<body>
<p><button onclick="clickCounter()" type="button">點(diǎn)擊</button></p>
<div id="result"></div>
<p>單擊該按鈕查看計(jì)數(shù)器增加。</p>
<p>關(guān)閉瀏覽器選項(xiàng)卡(或窗口),再試一次,計(jì)數(shù)器將繼續(xù)計(jì)數(shù)(不是重置)。</p>
</body>
</html>

執(zhí)行程式嘗試


sessionStorage 物件

sessionStorage 方法針對一個(gè)session 進(jìn)行資料儲(chǔ)存。當(dāng)使用者關(guān)閉瀏覽器視窗後,資料會(huì)被刪除。

如何建立並存取一個(gè) sessionStorage::

<!DOCTYPE html>
<html>
<head> 
    <meta charset="UTF-8">
    <title>php中文網(wǎng)(php.cn)</title> 
    <script>
        function clickCounter()
        {
            if(typeof(Storage)!=="undefined")
            {
                if (sessionStorage.clickcount)
                {
                    sessionStorage.clickcount=Number(sessionStorage.clickcount)+1;
                }
                else
                {
                    sessionStorage.clickcount=1;
                }
                document.getElementById("result").innerHTML="點(diǎn)擊按鈕 " + sessionStorage.clickcount + " time(s) ";
            }
            else
            {
                document.getElementById("result").innerHTML="對不起,您的瀏覽器不支持web存儲(chǔ)……";
            }
        }
    </script>
</head>
<body>
<p><button onclick="clickCounter()" type="button">點(diǎn)擊</button></p>
<div id="result"></div>
<p>單擊該按鈕查看計(jì)數(shù)器增加。</p>
<p>關(guān)閉瀏覽器選項(xiàng)卡(或窗口),再試一次,計(jì)數(shù)器復(fù)位</p>
</body>
</html>

執(zhí)行程式嘗試



##

繼續(xù)學(xué)習(xí)
||
<!DOCTYPE html> <html> <head>  <meta charset="UTF-8"> <title>php中文網(wǎng)(php.cn)</title>  <script> function clickCounter() { if(typeof(Storage)!=="undefined") { if (localStorage.clickcount) { localStorage.clickcount=Number(localStorage.clickcount)+1; } else { localStorage.clickcount=1; } document.getElementById("result").innerHTML="點(diǎn)擊按鈕" + localStorage.clickcount + " time(s)."; } else { document.getElementById("result").innerHTML="對不起,您的瀏覽器不支持web存儲(chǔ)……"; } } </script> </head> <body> <p><button onclick="clickCounter()" type="button">點(diǎn)擊</button></p> <div id="result"></div> <p>單擊該按鈕查看計(jì)數(shù)器增加。</p> <p>關(guān)閉瀏覽器選項(xiàng)卡(或窗口),再試一次,計(jì)數(shù)器將繼續(xù)計(jì)數(shù)(不是重置)。</p> </body> </html>
提交重置程式碼