<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>測試Web Storage part1</title>
</head>
<body>
html5 Web Storage可以用Local Storage或Session Storage兩種方式取代cookie跟seession的寫法。
</br>
<div id="result"></div>
<p><button onclick="clickCounter()" type="button">SessionStorage 點我計數!</button></p>
<script>
if(typeof(Storage)!=="undefined")
{
localStorage.aa="測試教程part1"; /*以Key-Value的型式儲存*/
document.getElementById("result").innerHTML="站名:" + localStorage.aa;
}
else
{
document.getElementById("result").innerHTML="您的browser不支援 web Storage。";
}
function clickCounter()/*SessionStorage範例*/
{
if(typeof(Storage)!=="undefined")
{
if (sessionStorage.clickcount)
{
sessionStorage.clickcount=Number(sessionStorage.clickcount)+1;
}
else
{
sessionStorage.clickcount=1;
}
document.getElementById("result").innerHTML="已點按鈕 " + sessionStorage.clickcount + " 次 ";
}
else
{
document.getElementById("result").innerHTML="抱歉,您的browser不支援Session Storage";
}
}
</script>
</body>
</html>