I have made a webpage that asks user's that if they want to enable an effect, if they say yes then i store that "YES" as a cookie effect=true
or if they don't want it then i also store it as effect=false
cookie. The problem is listed as steps below:
- User visits a webpage (first time)
- He is alerted with
document.cookie
which is "". - He Clicks OK
- Now value of
saveedd
is "", So according to my code the code below// Checkpoint 2
Should execute as value of saved is neithertrue
norfalse
. - But instead It executes only 1 line of code below
// Checkpoint 1
, This should not happen as value ofsaveed
is notfalse
.
What am I doing Wrong?
Note : I have stripped some code, So if you find any mistakes like var
not defined ,Please comment
index.html
<html>
<head>
<title>My Second Page</title>
</head>
<body onload="askk()">
</body>
</html>
script.js
function askk() {
var saveedd = getCookie('effect');
alert(saveedd);
if (saveedd == true) {
changge();
alert('i wil change');
} else if (saveedd == false) {
// Checkpoint 1
alert('i will not change');
} else {
// Checkpoint 2
document.cookie = "effect=;expires=Wed; 01 Jan 1970";
var z = confirm("Enable Bgcolor Effect?");
if (z) {
changge();
}
setCookie("effect", z, 30);
}
}
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) != -1) {
return c.substring(name.length, c.length);
}
}
return "";
}
function checkCookie() {
var user = getCookie("username");
if (user != "") {
alert("Welcome again " + user);
} else {
user = prompt("Please enter your name:", "");
if (user != "" && user != null) {
setCookie("username", user, 30);
}
}
}
function changge() {
window.setInterval("Farbe()", 500);
}
farbe = 1;
function Farbe() {
if (farbe == 1) {
document.bgColor = "indigo";
farbe = 2;
} else if (farbe == 2) {
document.bgColor = "red";
farbe = 3;
} else if (farbe == 3) {
document.bgColor = "green";
farbe = 4;
} else if (farbe == 4) {
document.bgColor = "blue";
farbe = 1;
}
}