I have a quiz in several HTML pages (I know it could be in only one but it has to be a page for question - q1, q2, q3...) with 4 radio buttons in each one (for q1: q1a, q1b, q1c, q1d; for q2: q2a, q2b, q2c, q2d, etc), and I want to set a cookie in each page with the name of the question (q1) and the checked answer (q1a, for example). I have created this function to set a cookie:
function set_cookie ( name, value ) {
var cookie_string = name + "=" + escape ( value );
document.cookie = cookie_string;
}
And I have put it in each page of the quiz. It works fine in each page, but when in the final page I use a function to get the cookies, it returns null. The function I have is this one:
function get_cookie ( cookie_name ) {
var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
if ( results )
return ( unescape ( results[2] ) ) ;
else
return null;
}
But when I call this function with the function to make the final grade of the quiz, it returns null:
function grade () {
var grade = 0;
var x = get_cookie( "q1" );
var y = get_cookie( "q2" );
alert(x); //Just to see if the function works fine
...
var xx = 0;
var yy = 0;
...
if(x == "q1a") xx = 20; //correct answer
else xx = 0; //wrong answers
if(y == "q2c") yy = 20; //correct answer
else yy = 0; //wrong answers
...
...
grade = xx + yy;
alert("Your qualification is "+grade);
}
What am I doing wrong? Is it not possible to get the cookies from one page in another one if both pages are in the same directory?