javaScript--7 cookies設(shè)置與獲取
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Cookies</title>
</head>
<head>
<script>
// 設(shè)置cookie
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
//添加Cookie
function addCookie() {
var cookieName = document.getElementById("setCookie").value;
var cookieValue = document.getElementById("param").value;
var cookieEx = document.getElementById("time").value;
setCookie(cookieName, cookieValue, cookieEx);
}
// 獲取cookies
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i].trim();
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "無數(shù)據(jù)";
}
//查詢Cookie
function loadCookie() {
var cookieName = document.getElementById("getCookie").value;
var cookieValue = getCookie(cookieName);
document.getElementById("spnCookies").innerText = cookieValue;
console.log(cookieName + "====" + cookieValue);
}
</script>
</head>
<body>
設(shè)置cookies:<br />
名稱:<input type="text" id="setCookie" /> 參數(shù):<input type="text" id="param" /> 有效時間:<input type="number" id="time">
<input type="button" value="添加Cookie" onclick="addCookie()" /><br>
獲取cookies:<br />
名稱:<input type="text" id="getCookie" /> <span id="spnCookies"></span><br>
<input type="button" value="獲取Cookie"
onclick="loadCookie()" />
</body>
</html>
創(chuàng)作不易,轉(zhuǎn)摘請標(biāo)明出處。如果有意一起探討測試相關(guān)技能可加博主QQ 771268289 博主微信:ding17121598
本文來自博客園,作者:怪圣卡杰,轉(zhuǎn)載請注明原文鏈接:http://www.rzrgm.cn/dwdw/p/16750411.html
浙公網(wǎng)安備 33010602011771號