I am having a trouble with JavaScript cookie. I run this code last time and it worked like what I want to. It shows a prompt letting me to enter my name, and clicks OK after entering my name. It also worked when I refreshed the page and shows an alert message saying " Wele again Mark". But now, this code does not work anymore. It only ask and ask my name everytime I refresh the page. I'm doubting if it's on my browser I already tries tor un this code in IE, Firefox, Chrome, and MS Edge but it does the same thing. Hope you cans olve the problem guys. Thanks. The code is below:
<html>
<head>
<script type="text/javascript">
function setCookie(cname, cvalue, exdays)
{
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
function getCookie(cname)
{
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i = 0; i < ca.length; i++)
{
var c = ca[i];
while(c.charAt(0) == ' ')
{
c = c.substring(1);
}
if(c.indexOf(name) == 0)
{
return c.substring(name.length,c.length);
}
}
return "";
}
function checkCookie()
{
var username = getCookie("username");
if(username != null && username != "")
{
alert("Wele again " + username);
} else
{
username = prompt("Please enter your name:" , "");
if(username != null && username != "")
{
setCookie("username" , username , 365);
}
}
}
</script>
</head>
<body onload="checkCookie()">
</body>
</html>
I am having a trouble with JavaScript cookie. I run this code last time and it worked like what I want to. It shows a prompt letting me to enter my name, and clicks OK after entering my name. It also worked when I refreshed the page and shows an alert message saying " Wele again Mark". But now, this code does not work anymore. It only ask and ask my name everytime I refresh the page. I'm doubting if it's on my browser I already tries tor un this code in IE, Firefox, Chrome, and MS Edge but it does the same thing. Hope you cans olve the problem guys. Thanks. The code is below:
<html>
<head>
<script type="text/javascript">
function setCookie(cname, cvalue, exdays)
{
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
function getCookie(cname)
{
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i = 0; i < ca.length; i++)
{
var c = ca[i];
while(c.charAt(0) == ' ')
{
c = c.substring(1);
}
if(c.indexOf(name) == 0)
{
return c.substring(name.length,c.length);
}
}
return "";
}
function checkCookie()
{
var username = getCookie("username");
if(username != null && username != "")
{
alert("Wele again " + username);
} else
{
username = prompt("Please enter your name:" , "");
if(username != null && username != "")
{
setCookie("username" , username , 365);
}
}
}
</script>
</head>
<body onload="checkCookie()">
</body>
</html>
Share
Improve this question
edited Dec 28, 2017 at 9:06
Mark Cay
asked Dec 28, 2017 at 8:33
Mark CayMark Cay
331 silver badge10 bronze badges
4
- Any error in console? – Frederik.L Commented Dec 28, 2017 at 8:35
- I am sorry, I do not quietly understand it. You mean if Console works properly in my browser? – Mark Cay Commented Dec 28, 2017 at 8:38
-
I meant if there was any errors showing up in the console when you open the page. Also, in your code why do you define a variable
d
insetCookie
if you don't use it afterwards? Is it meant to be used forexpires
? – Frederik.L Commented Dec 28, 2017 at 9:02 - Actually it's: var expires = "expires=" + d.toGMTString(); – Mark Cay Commented Dec 28, 2017 at 9:04
2 Answers
Reset to default 3var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 *
1000));
var expires = "expires=";
document.cookie = cname + "=" + cvalue + "; " +
expires;
Your cookie string doesn't include the date. You forgot the + d
.
Your issue is probably this line:
document.cookie = cname + "=" + cvalue + "; " +
expires;
You forgot adding the date:
document.cookie = cname + "=" + cvalue + "; " +
expires + d;
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745646513a4638002.html
评论列表(0条)