I've recently created a small galiga like game recently using JavaScript and HTML5. I've run into a bit of trouble saving cookies, the cookie saves, but then resets itself when the page is refreshed, my code:

function saveScore() {
var date = new Date();
date.setMonth(date.getMonth()+5);
var expires = "; expires=" + date.toGMTString();
document.cookie = "score=" + lvl + expires + "; path=/";
}

for (var i = 0; i < cookiearray.length; i++) {
if (name == "score") {
lvl = value;
}
}
}


EDIT: It appears as though I made an error outside of the given code, and reset the level to zero, I fixed this, and everything works well, I apologize for any confusion caused.

• Are you sure you're actually calling the loadScore function? – Tetrad Jan 23 '13 at 18:51
• Could your JavaScript loadScore() function be throwing an exception? I would think that you can't get the score because of an 'index out of bounds' exception when you use "split('=')[0]" try checking the make sure that the '=' char is there, also please let us know if the alert is being executed. – Benjamin Danger Johnson Jan 23 '13 at 18:53
• The alert is never being executed even though I run loadScore right after the variable lvl is defined. – Dylan Katz Jan 23 '13 at 18:54
• definitely check for an exception. alert yourself with the unsplit cookiearray element in the for loop so you can check exactly what values you are receiving and possibly where things are blowing up. If it is like I thought with the 'out of bounds' exception, then you can easily fix the method by checking if the string contains a '=' char. – Benjamin Danger Johnson Jan 23 '13 at 19:05
• Ok, Sorry, it appears as though the alert is working, and I also added alerts for the value of score, and it comes out correctly, it seems as though the level is somehow resetting itself, I've also attempted to set the player health to the level, but it seems to stay the same, so it does not appear to be a problem with the lvl variable. – Dylan Katz Jan 23 '13 at 19:43

ddocument.cookie = "score=" + lvl + expires + "; path=/";