我试图设置一个cookie,但我无法得到它的工作。
也有人请你指出为什么?
当点击了函数create_cookie被调用,但不是该Cookie。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title>JS Testing Page</title>
<script type="text/javascript">
onError=errorhandler;
function errorhandler(msg,url,l)
{
text = "There was an error. n";
text+= "The error was : " + msg + " n";
text+= "It happened on " + url + " n ";
text+= "on line " + l + ".";
alert(text);
}
function readCookie()
{
var cvalue = document.cookie;
if(cvalue !== null || cvalue!= "")
{
return cvalue;
}
else
{
return false;
}
}
function delete_cookies()
{
var cnames = document.cookie;
for(var i in cnames)
{
var exp_Date = new Date();
document.cookie = cnames.i + "=""; expires=" + (exp_Date-1) + ";";
}
return true;
}
function create_cookie()
{
var exp_Date = new Date();
/*document.write(exp_Date);*/
document.cookie = "test=yes; expires="+exp_Date+";";
document.write("Lo <br />");
return true;
}
</script>
</head>
<body>
<script type="text/javascript">
var x = readCookie();
document.write(x);
if((x != "") && (x !== null) && (x !== false))
{
document.write("<br /> The cookie that has been set was : " + x);
document.write("<br /><a href="#" onclick="delete_cookies(); document.location.reload();">Clear cookies stored by this site.</a>");
}
else if( x== "" || x === null || x === false)
{
document.write("<br /> <a href="#" onclick="create_cookie()">Set</a> the cookie.");
alert("Hello");
}
</script>
</body>
</html>
您没有设置Cookie失效日期,正确。
设置为在特定天数到期使用
var exp_Date = new Date ();
var days =1;
exp_Date.setTime(exp_Date.getTime()+(days*24*60*60*1000));
document.cookie = "test=yes; expires=" + exp_Date.toGMTString();
to last till your current session only, remove all code for date, and just use
document.cookie = "test=yes;";
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>JS Testing Page</title>
<script type="text/javascript">
function errorhandler(msg,url,l)
{
text = "There was an error. n";
text+= "The error was : " + msg + " n";
text+= "It happened on " + url + " n ";
text+= "on line " + l + ".";
alert(text);
}
onError=errorhandler;
function readCookie()
{
alert("ja");
var cvalue = document.cookie;
if(cvalue!==false || cvalue!==null || cvalue!='')
{
if(cvalue !== null || cvalue != "")
{
return cvalue;
}
else
{
return false;
}
}
}
function delete_cookies()
{
//var cnames/*_data*/ = document.cookie;
//var cnames = cname_data.split('=');
if(document.cookie !== null && document.cookie != "")
{
var line = document.cookie.indexOf("=");
var begin = 0;
var cname = document.cookie.slice(begin,line);
var exp_Date = new Date();
exp_Date.setTime(exp_Date.getTime()-1000*60*60);
if(document.cookie.indexOf('=') == document.cookie.lastindexOf('='))
{
document.cookie = cname + "=''; expires=" + exp_Date + ";";
//alert("Cookie " + cname + " has been deleted.");
}
else
{
var cookieval = cname + "=""; ";
cookieval += "expires=" + exp_Date + ";";
document.cookie = cookieval;
cname = document.cookie;
if(cname =='' || cname === false || cname === null)
{
break;
}
}
return true;
}
}
function create_cookie(cookval)
{
var exp_Date = new Date();
exp_Date.setTime(exp_Date.getTime()+1000*60*60);
/*document.write(exp_Date);*/
var cookieval = "user=" + cookval + "; ";
cookieval += "expires=" + exp_Date + ";";
document.cookie = cookieval;
return true;
}
</script>
</head>
<body>
<script type="text/javascript">
var x = readCookie();
if(x != "" && x !== null && x !== false)
{
document.write("<br /> The cookie that has been set was : " + x);
document.write("<br /><a href="#" onclick="delete_cookies(); document.location.reload();">Clear cookies stored by this site.</a>");
}
else if( x== "" || x === null || x === false)
{
document.write("<br /> <a href="#" onclick="create_cookie('cookie')">Set</a> the cookie.");
}
</script>
</body>
</html>