close

[JavaScript HTML DOM]使用document物件,使用屬性document.cookie之一

說明:
document.cookie 存入cookie資料
document.cookie=cookie資料名稱=資料;expires=有效期限
此範例設計說明:
第一步、setCookie(資料的名稱, 存放的值, 有效期限天數)如:setCookie("test", "levin", 1);
第二步、設定有效期限
第三步、存入document.cookie=cookie資料名稱=資料;expires=有效期限
第四步、顯示寫入 Cookie 成功頁面上
程式:
<html>
<head>
<title>[JavaScript-HTML_DOM]使用document物件,使用屬性document.cookie之一</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script>
function setCookie(pname, pvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toGMTString();
document.cookie = pname + "=" + pvalue + "; " + expires;
document.getElementById("returnData").innerHTML = "寫入 Cookie 成功。";
}
function mytest() {
var returnData = "";
setCookie("test", "levin", 1);
}
</script>
</head>
<body>
[JavaScript-HTML_DOM]使用document物件,使用屬性document.cookie之一
<br>
<input type="button" value="測試" onclick="mytest();" />
<span id="returnData"></span>
</body>
</html>
圖:


按下測試


相關範例:
待續

參考:

JavaScript Cookies



 






 















其它文章

arrow
arrow

    PG Levin Li 發表在 痞客邦 留言(0) 人氣()