<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>cookie-set-get-del</title>
<script>
// 设置cookie:
function setCookie (key, value, expires) {
var date = new Date();
date.setTime(date.getTime() + expires*24*3600*1000);
if ( !expires ) {
document.cookie = key + '=' + value;
} else {
document.cookie = key + '=' + value + ';expires=' + date.toGMTString() +';path=/' ;
}
}
//读取cookie:
function getCookie (key) {
var cookies = document.cookie;
var acookie = cookies.split('; ')
for ( var i = 0; i < acookie.length; i++ ) {
var cookie = acookie[i].split('=');
if ( cookie[0] == key) {
// alert(cookie[1]);
return cookie[1];
}
}
}
// 删除cookie:
function delCookie (key) {
setCookie(key, '', -1);
}
</script>
</head>
<body>
<input type="button" value="set cookie" onclick="setCookie('name','1234',10)">
<input type="button" value="get cookie" onclick="getCookie('name')">
<input type="button" value="del cookie" onclick="delCookie('name')">
</body>
</html>