javascript中的cookie和session設置

//cookie設置html

 

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script>

function getCookie(c_name) {
if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(c_name + "=");
if (c_start != -1) {
c_start = c_start + c_name.length + 1;
c_end = document.cookie.indexOf(";", c_start);
if (c_end == -1) c_end = document.cookie.length;
return unescape(document.cookie.substring(c_start, c_end));
}
}
return "";
}

function setCookie(c_name, value, expiredays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + expiredays);
document.cookie = c_name + "=" + escape(value) +
((expiredays == null) ? "" : "; expires=" + exdate.toGMTString())
}

function checkCookie() {
username = getCookie('username');
if (username != null && username != "") {
alert('Welcome again ' + username + '!')
}
else {
username = prompt('Please enter your name:', "");
if (username != null && username != "") {
setCookie('username', username, 30)
}
}
}
</script>
</head>
<body onload="checkCookie()">
<script>
//建立和存儲cookie
alert((new Date()).toGMTString());
//cookie是存儲於訪問者的計算機中的變量,每當同一臺計算機經過瀏覽器請求頁面時,就會發送這個cookie
</script>
</body>
</html>

//session設置
<!doctype html><html lang="en"><head>    <meta charset="UTF-8">    <title>本地存儲sessionStorsge</title></head><body><input type="text" id="msg"><input type="button" id="setData" value="保存數據"><input type="button" id="getData" value="獲取數據"><input type="button" id="removeData" value="清除數據"><input type="button" id="clearData" value="所有清空"><script>    window.onload= function () {        alert("當你關閉此頁面或者關閉瀏覽器的時候,sessionStorage中保存的數據纔會消失,也就是說從新打開此頁面的時候,點擊獲取數據  ,將不會顯示任何數據,刷新頁面無效。\r\n由此能夠證實,sessionStorage的生命週期爲,某個用戶瀏覽網站時,從進入到離開的這段時間。");        var msg=document.getElementById('msg');        var setData=document.getElementById('setData');        var getData=document.getElementById('getData');        var removeData=document.getElementById('removeData');        var clearData=document.getElementById('clearData');        setData.onclick= function () {//存入數據            if(msg.value){                sessionStorage.setItem('data',msg.value);                alert('信息已保存中data字段中');            }else {                alert('信息不能爲空')            }        };        getData.onclick= function () {//獲取數據            var msg = sessionStorage.getItem('data');            if(msg){                alert('data字段中的值爲'+msg);            }            else {                alert('data字段無值');            }        };        removeData.onclick= function () {            var msg = sessionStorage.getItem('data');            if(msg){                sessionStorage.removeItem('data');            }else {                alert('data字段中的數值爲空')            }        };        clearData.onclick=function(){            sessionStorage.clear();        }    };    //頁面自動刷新//    function myrefresh(){//        window.location.reload();//        alert('1')//    }//    setTimeout(myrefresh,1000)</script></body></html>
相關文章
相關標籤/搜索