1

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:

  1. User visits a webpage (first time)
  2. He is alerted with document.cookie which is "".
  3. He Clicks OK
  4. Now value of saveedd is "", So according to my code the code below // Checkpoint 2 Should execute as value of saved is neither true nor false.
  5. But instead It executes only 1 line of code below // Checkpoint 1 , This should not happen as value of saveed is not false.

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;
    }
}
Ashesh Kumar
  • 223
  • 2
  • 12

2 Answers2

1

In comparison if (saveedd == false) string saveedd casting to bool type, empty string in JS casting to false. You should compare string with string like this: (saveedd == 'false')

weeklyTea
  • 316
  • 2
  • 10
1

You are comparing with the == operator, which coerces variables to be the same type before comparison. Hence, the value saveedd, which is the empty string '' will be converted to false. This is why Checkpoint 1 is being executed.

You will need to compare using the triple equals ===. This will preserve the type of the variables you are comparing, so saveedd == false will only be true if saveedd is false, not 0, '', [] or another "falsy" value. See Which equals operator (== vs ===) should be used in JavaScript comparisons? for more information.

Edit:

As weeklyTea mentions, you are also comparing against the boolean true and false. You may need to compare against the strings "true" and "false" instead.

Community
  • 1
  • 1
David Hughes
  • 381
  • 1
  • 12