var answers = ['B','B','B','B','B','B'];
var score=0;
var total = answers.length;
function getCheckedValues(radioName) {
var radios=document.getElementsByName(radioName);
for(var i=0;i<radios.length;i++) {
if(radios[i].checked) return radios[i].value;
}
}
function check_score() {
for(var i=0;i<total;i++) {
if(answers[i]==getCheckedValues("question"+i))
score++;
}
returnScore();
}
function returnScore() {
window.open("score_display.php");
}
window.onload=function congratulations_message() {
if(score>3)
document.getElementById('score-display').innerHTML="<h1>Your Score is:</h1><h2>"+score+"</h2>"+"<h1>Congratulations, You are on a path to glory!!!</h1>";
else {
document.getElementById('score-display').innerHTML="<h1>Your Score is:</h1><h2>"+score+"</h2>"+"<h1>Oops!!! Hard Luck! Better luck next time</h1>";
}
}
In the above code, I'm using the variable score to calculate the score of a quiz game. When I move onto "score_display.php", another page to display the 'score' as a congratulatory message, the value of score becomes 0. I assumed that since score is a global variable it can be used across the different html files that I linked my javascript file to. Am I wrong in my assumption?
Is there any way I can retain the value of 'score'?