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

HTML5 Web 存儲(chǔ)

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

使用HTML5可以在本地存儲(chǔ)用戶的瀏覽數(shù)據(jù),是一個(gè)比cookie更好的本地存儲(chǔ)方式。

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

數(shù)據(jù)以 鍵/值 對(duì)存在, web網(wǎng)頁(yè)的數(shù)據(jù)只允許該網(wǎng)頁(yè)訪問使用。


瀏覽器支持

8.jpg

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

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


localStorage 和 sessionStorage?

有兩個(gè)新對(duì)象將數(shù)據(jù)存儲(chǔ)在客戶端:

  • localStorage - 沒有時(shí)間限制的數(shù)據(jù)存儲(chǔ)

  • sessionStorage - 針對(duì)一個(gè) session 的數(shù)據(jù)存儲(chǔ)

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

if(typeof(Storage)!=="undefined")
{
? ?// sessionStorage localStorage 支持!
? ?// 相關(guān)代碼.....
}
else
{
? ?// 對(duì)不起,不支持Web 存儲(chǔ)
}


localStorage 對(duì)象

localStorage 對(duì)象存儲(chǔ)的數(shù)據(jù)沒有時(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="對(duì)不起,您的瀏覽器不支持web存儲(chǔ)……";
    }
</script>
</body>
</html>

運(yùn)行程序嘗試一下

實(shí)例解析:

使用 key="lastname" 和value="Smith" 創(chuàng)建一個(gè) localStorage 鍵/值對(duì)

檢索鍵值為"lastname" 的值然后將數(shù)據(jù)插入 id="result"的元素中

提示:?鍵/值對(duì)通常以字符串存儲(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="對(duì)不起,您的瀏覽器不支持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>

運(yùn)行程序嘗試一下


sessionStorage 對(duì)象

sessionStorage 方法針對(duì)一個(gè) session 進(jìn)行數(shù)據(jù)存儲(chǔ)。當(dāng)用戶關(guān)閉瀏覽器窗口后,數(shù)據(jù)會(huì)被刪除。

如何創(chuàng)建并訪問一個(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="對(duì)不起,您的瀏覽器不支持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>

運(yùn)行程序嘗試一下



繼續(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="對(duì)不起,您的瀏覽器不支持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>
提交重置代碼