Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im implementing cookies to store data for a game. This data is in small amounts so cookies should be the right idea. Ive implemented two functions in my code for cookies.
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].trim();
        if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
    }
    return "";
}

I then put in a check to see if the cookies are set, according to other sources this should work:
JavaScript
if(typeof cookieexists === null){
	starttimer();
}else{
	kernels = getcookie("kernels");
	xtrakernels = getcookie("xtrakernels");
	nbprice = getcookie("nbprice");
	ppprice = getcookie("ppprice");
	kps = getcookie("kps");
	apprice = getcookie("apprice");
	kcprice = getcookie("kcprice");
	starttimer();
}

starttimer() calls on kernps() every second, here is kernps()

JavaScript
function kernps() {
	kernels = kernels + kps;
	document.getElementById('number').innerHTML = kernels;
	setcookie("kernels", kernels, "100");
	setcookie("xtrakernels", xtrakernels, "100");
	setcookie("nbprice", nbprice, "100");
	setcookie("ppprice", ppprice, "100");
	setcookie("kps", kps, "100");
	setcookie("apprice", apprice, "100");
	setcookie("kcprice", kcprice, "100");
}


along with all this, i have declared my variables (Before all of this)
JavaScript
var cookieexists = getcookie("kernels");
var kernels = 1;
var xtrakernels = 1;
var nbprice = 50;
var ppprice = 250;
var kps = 0;
var apprice = 75;
var kcprice = 275;

What happens is all the variables end up as null.

I have no idea where to start as to what is going on.

NOTE: I have tried and cleared my cookies if that matters. That did not work.
Posted
Updated 7-Jun-14 9:06am
v2

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