Code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script language="JavaScript" type="text/javascript">
<!--
function addCookie(name,value,expireHours)
{
var cookieString=name+"="+escape(value);
//判断是否设置过期时间
if(expireHours>0){
var date=new Date();
date.setTime(date.getTime+expireHours*3600*1000);
cookieString=cookieString+"; expire="+date.toGMTString();
}
document.cookie=cookieString;
alert(document.cookie);
}
function getCookie(name)
{
var strCookie=document.cookie;
var arrCookie=strCookie.split("; ");
var abc="";
for(var i=0;i<arrCookie.length;i++)
{
var arr=arrCookie[i].split("=");
if(arr[0]==name)
abc=arr[1];
alert(abc);
}
}
//-->
</script>
</head>
<body onload="getCookie('aa')">
<input type="button" value="addcookie" onclick="addCookie('aa','bb',1)"/>
<input type="button" value="getcookie" onclick="getCookie('aa')"/>
</body>
</html>
帮我看下这段代码,cookie设置的时间没有用,刷新后是有值的,关了再开 这个值就不在了~~
问题补充:
这是源代码;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title>
<script language="JavaScript" type="text/javascript">
<!--
function addCookie(name,value,expireHours)
{
var cookieString=name+"="+escape(value);
//判断是否设置过期时间
if(expireHours>0)
{
var date=new Date();
date.setTime(date.getTime()+expireHours*3600*1000);
cookieString=cookieString+"; expire="+date.toGMTString();
}
document.cookie=cookieString;
alert(document.cookie);
}
function getCookie(name)
{
var strCookie=document.cookie;
var arrCookie=strCookie.split("; ");
var s ="";
for(var i=0;i<arrCookie.length;i++)
{
var arr=arrCookie[i].split("=");
if(arr[0]==name)
s= arr[1];
alert(s);
}
return "";
}
//-->
</script>
</head>
<body >
<input type="button" value="addCookie" onclick="addCookie('aa','bb',1)" />
<input type="button" value="getcookie" onclick="getCookie('aa')"/>
</body>
</html>