...

View Full Version : localStorage storing value as 11111?



whatchama
04-07-2011, 04:26 AM
Hi all!

In the code I am writing, when an image is clicked it sends a particular value to the function "redClick" which is in javascript.

I am then trying to get the localStorage value for the key scoreRed. I am then clearing it, and then increasing the score by 1 and then storing it locally again.

For some reason, I am not able to clear it using the localStorage.removeItem code, and when it does increment it, it appends a 1 to the score rather than increasing it. i.e. the score ends up being 11111 rather than 5.

The code I have is below:


function redClick( buttonLocation ) {
if(buttonLocation == redLocation){
var redScore = localStorage.getItem("scoreRed"); //this is retrieving the score (scoreRed) from a previous game: lets say 5 and assigning it to redScore
localStorage.removeItem("scoreRed");
redScore = redScore +1; //this is incrementing the score, so now redScore = 6
localStorage.setItem("scoreRed", redScore); //this is saving the score - 6 - to a localstorage var called scoreRed
//window.location.reload();
}
}

Any and all help would be much appreciated.

whatchama
04-07-2011, 04:40 AM
Just got some of the code to work. The new code is below:


function redClick( buttonLocation ) {
if(buttonLocation == redLocation){
var redScore = localStorage.getItem("scoreRed");
//this is retrieving the score (scoreRed) from a previous game: lets say 5 and assigning it to redScore
redScore = (redScore*1) +1; //this is incrementing the score, so now redScore = 6
localStorage.setItem("scoreRed", redScore); //this is saving the score - 6 - to a localstorage var called scoreRed
localStorage.removeItem("scoreRed");
}
}


I cant seem to get the removeItem part of the code to work. Any ideas?

eberdome
04-07-2011, 05:41 AM
I would re-post this under Javascript as it has nothing to do with HTML and/or CSS!!

whatchama
04-07-2011, 08:40 AM
Actually localStorage is a HTML5 feature, hence I was posting it in this forum.

Either way, I solved it myself. Thanks!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum