来自CSDN.NET,http://www.bobopo.com/article/1000/84.htm
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Cookie处理函数练习</title>
<script language="JavaScript" type="text/javascript">
var $=function(el){
return (typeof el=='object')?el:document.getElementById(el);
};
var addCookie=function(objName,objValue,objHours){//添加cookie
var str = objName + "=" + escape(objValue);
if(objHours > 0){//为0时不设定过期时间,浏览器关闭时cookie自动消失
var date = new Date();
var ms = objHours*3600*1000;
date.setTime(date.getTime() + ms);
str += "; expires=" + date.toGMTString();
}
document.cookie = str;
alert("添加cookie成功");
};
var getCookie=function(objName){//获取指定名称的cookie的值
var arrStr = document.cookie.split("; ");
for(var i = 0;i < arrStr.length;i ++){
var temp = arrStr[i].split("=");
if(temp[0] == objName){
return unescape(temp[1]);
}
}
};
var delCookie=function(name){//为了删除指定名称的cookie,可以将其过期时间设定为一个过去的时间
var date = new Date();
date.setTime(date.getTime() - 10000);
document.cookie = name + "=a; expires=" + date.toGMTString();
alert("删除成功");
};
var add=function(){//添加Cookie
var cookie_name = $("cookie_name").value;
var cookie_value = $("cookie_value").value;
var cookie_expireHours = $("cookie_expiresHours").value;
if(cookie_name==""||cookie_value==""){
alert("请填写完整Cookie的名称和值");
return;
}
addCookie(cookie_name,cookie_value,cookie_expireHours);
};
var getall=function(){//读取所有保存的cookie字符串
var str = document.cookie;
if(str == ""){
str = "没有保存任何cookie";
}
alert(str);
};
var get=function(){//读取单个该名称的cookie
var cookie_name = $("cookie_name").value;
var cookie_value = getCookie(cookie_name);
if(cookie_name ==""){
alert("请填写名称,根据名称进行查找");
return;
}
if(cookie_value==null){
cookie_value="没有该名称的cookie";
}
alert(cookie_value);
};
var del=function(){//删除该名称的cookie
var cookie_name = $("cookie_name").value;
if(cookie_name==""){
alert("请填写名称,根据名称进行删除");
return;
}
delCookie(cookie_name);
};
</script>
</head>
<body>
<form name="myform">
<div><label for="cookie_name">Cookie名称:</label><input type="text" name="cookie_name" /></div>
<div><label for="cookie_value">Cookie 值:</lable><input type="text" name="cookie_value" /></div>
<div><label for="cookie_expireHours">过期时间(小时):</lable><input type="text" name="cookie_expiresHours" /></div><hr/>
<div>
<input type="button" value="添加该cookie" onclick="add()" />
<input type="button" value="读取所有cookie" onclick="getall()" />
<input type="button" value="读取该名称cookie的值" onclick="get()" />
<input type="button" value="删除该名称cookie" onclick="del()" />
</div>
</form>
<hr />
</body>
</html>
-------------------------------------------------------------------------------------
cookie只需要保存name 和value 和 时间就行了!
我给你写的方法里有
var addCookie=function(objName,objValue,objHours){//添加cookie
var str = objName + "=" + escape(objValue);
if(objHours > 0){//为0时不设定过期时间,浏览器关闭时cookie自动消失
var date = new Date();
var ms = objHours*3600*1000;
date.setTime(date.getTime() + ms);
str += "; expires=" + date.toGMTString();
}
document.cookie = str;
alert("添加cookie成功");
};