我试图创建一个简单的程序,如果设置了cookie,则显示欢迎消息,如果没有设置cookie,它将显示一个提示框,询问用户名。
它不起作用,我不明白为什么。谁能告诉我代码中的问题在哪里?
以下是代码:
<!DOCTYPE html>
<html>
<head>
<script>
function setCookie(c_name,value,expiredays){
var exdate = new Date();
exdate.setDate(exdate.getDate()+expiredays);
if(expiredays==null)
document.cookie = c_name + "=" + escape(value) +"";
else
document.cookie = c_name + "=" + escape(value) + ";expires="
+exdate.toGMTString());
}
function getCookie(c_name){
if(document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=");
if(c_start!=-1)
c_start=c_start + c_name.length +1;
//index of the value's end
c_end=document.cookie.indexOf(";",c_start);
if(c_end==-1)//if it's the last cookie
c_end = document.cookie.length;
return unescape (document.cookie.substring(c_start,c_end));
}
}
return "";
}
function checkCookie(){
username=getCookie('username');
if(username!=null && username!="")
alert('welcome again ' + username+ '!');
else {
username=prompt('please enter your name:',"");
if(username!=null && username!="")
setCookie('username',username,365);
}
}
</script>
</head>
<body onload="checkCookie()">
</body>
</html>
答案 0 :(得分:3)
您的代码中有两个错误。一个未闭合的“)”(在setCookie()
之后的toGMTString();
函数中)和一个未闭合的“}”(在getCookie()
函数中,紧跟在if(c_start!=-1){
之后)。
只需查看您的javascript控制台即可检查此错误。这是更正:
<html>
<head>
<script>
function setCookie(c_name,value,expiredays){
var exdate = new Date();
exdate.setDate(exdate.getDate()+expiredays);
if(expiredays==null)
document.cookie = c_name + "=" + escape(value) +"";
else
document.cookie = c_name + "=" + escape(value) + ";expires="
+exdate.toGMTString();
}
function getCookie(c_name){
if(document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=");
if(c_start!=-1){
c_start=c_start + c_name.length +1;
//index of the value's end
c_end=document.cookie.indexOf(";",c_start);
if(c_end==-1)//if it's the last cookie
c_end = document.cookie.length;
return unescape (document.cookie.substring(c_start,c_end));
}
}
return "";
}
function checkCookie(){
username=getCookie('username');
if(username!=null && username!="")
alert('welcome again ' + username+ '!');
else {
username=prompt('please enter your name:',"");
if(username!=null && username!="")
setCookie('username',username,365);
}
}
</script>
</head>
<body onload="checkCookie()">
za
</body>
</html>
答案 1 :(得分:1)
以下是代码的优化版本:
function getCookie(name) {
var setPos = document.cookie.indexOf(name + '='), stopPos = document.cookie.indexOf(';', setPos);
return !~setPos ? null : document.cookie.substring(
setPos, ~stopPos ? stopPos : undefined).split('=')[1];
}
function setCookie(name, val, days, path) {
var cookie = name + "=" + escape(val) + "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
cookie += ";expires=" + date.toGMTString();
}
cookie += ";" + (path || "path=/");
console.log(cookie);
document.cookie = cookie;
}
var username = getCookie("username");
if (!username) {
setCookie("username", prompt("please enter your name:"), 365);
} else {
alert("welcome again " + username);
}