<html>
<head>
<title> cookies操作 </title>
<script language="javascript" type="text/javascript">
function addCookie(name, value, hours) {
var str = name + "=" + escape(value);
if (hours > 0) {
var date = new Date();
var ms = hours * 60 * 60 * 1000;
date.setTime(date.getTime() + ms);
str += ";expires=" + date.toGMTString();
}
document.cookie = str;
alert ("添加cookie成功");
};
function getCookie(name) {
var arrStr = document.cookie.split(";");
for (var i = 0; i < arrStr.length; ++i) {
var temp = arrStr[i].split('=');
if (temp[0] == name) {
return unescape(temp[1]);
}
}
};
function delCookie(name) {
var date = new Date();
date.setTime(date.getTime() - 1000);
document.cookie = name + "=a;expires=" + date.toGMTString();
};
function allCookie() {
var str = document.cookie;
if (str != "") {
str = "没有任何cookie";
}
alert (str);
};
function saveCookie() {
var value = document.getElementById("tmp").value;
addCookie("test", value, 2);
};
function alertCookie() {
var value = getCookie("test");
alert(value);
};
</script>
</head>
<body>
<form>
<input type="text" id="tmp" name="tmp" />
<input type="button" value="保存" οnclick="saveCookie()" />
<input type="button" value="读取" οnclick="alertCookie()" />
</form>
</body>
</html>