Click here to Skip to main content
15,991,360 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone, its my first time posting question on this site. I have a java script asking the coming user for his name and pet name. Then display the name and pet name and number of times they visit this site, I am able to print the name and number of visit at each visit. But unable to print out the pet name. Its my first time working with cookies. Here is my code:

What I have tried:

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 + ";path=/";
}
function setCookie2(cpet,cvalue,exdays) {
    var e = new Date();
    e.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires=" + e.toGMTString();
    document.cookie = cpet + "=" + cvalue + ";" + expires + ";path=/";
}

function getCookie(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.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 getCookie2(cpet) {
    var pet = cpet + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var cr = decodedCookie.split(';');
    for(var i = 0; i < cr.length; i++) {
        var d = cr[i];
        while (d.charAt(0) == ' ') {
            d = d.substring(1);
        }
        if (d.indexOf(name) == 0) {
            return d.substring(name.length, d.length);
        }
    }
    return "";
}


function checkCookie() {
    var user=getCookie("username");
    var pet=getCookie2("pet");
    if (user != "") {
        alert("Welcome again " + user);
        document.getElementById("demo").innerHTML = "Welcome back, " + user;
        document.getElementById("d").innerHTML = "Your pet name, " + pet;
    } else {
       user = prompt("Please enter your name:","");
       pet = prompt("Please enter your pet name:","");
       if (user != "" && user != null) {
           setCookie("username", user, 30);
           setCookie2("pet", user, 30);
       }

    }
}
Posted
Updated 25-Feb-17 9:44am

1 solution

Couple thing, I think the getCookie2 and setCookie2 are redundant. and this line should be setCookie("pet", pet, 30); instead of setCookie("pet", user, 30);

JavaScript
function checkCookie() {
    var user = getCookie("username");
    var pet = getCookie("pet");
    if (user != "") {
        alert("Welcome again " + user + ' and your pet: ' + pet);
        document.getElementById("demo").innerHTML = "Welcome back, " + user;
        document.getElementById("d").innerHTML = "Your pet name, " + pet;
    } else {
        user = prompt("Please enter your name:", "");
        pet = prompt("Please enter your pet name:", "");
        if (user != "" && user != null) {
            setCookie("username", user, 30);
            setCookie("pet", pet, 30);
        }

    }
}


Output:
Welcome back, myname
Your pet name, mypet
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900