You can use cookies for this requirement - it's possible either to set a different cookie for each question or to store all questions/answers as value of a single cookie.
Not knowing the full request just an example: in case the quiz is a multiple choice where the value to be stored as answer is just a number or letter, the cookie value can
just start from 1=a (for question 1, answer a), 1=a&2=c and so on. In addition it's possible to parse the cookie value to check if an answer was already given, in case it's
allowed to change the first answer, and to adjust the cookie value accordingly.
For simplicity just an example for the case that every question would only allowed to be answered once.
First, check if the cookie already exists. E.g. using a cookie named _quizCookie:
function checkQuizCookie() {
var i,x,y,ARRcookies = document.cookie.split(";");
for (i = 0; i < ARRcookies.length; i++) {
x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
x = x.replace(/^\s+|\s+$/g, "");
if (x.indexOf("_quizCookie") != -1) {
return false;
}
}
}
Calling prepareQuizCookie()
when the page loaded:
function prepareQuizCookie() {
if (false != checkQuizCookie()) {
setQuizCookie();
}
}
So in case _quizCookie already exists, it won't be overwritten; in case it's not there, it will be created:
function setQuizCookie() {
var now = new Date(),
expireDate = new Date();
expireDate.setTime(now.getTime() + (10 * 365 * 24 * 60 * 60 * 1000));
document.cookie = "_quizCookie=start" + "; expires=" + expireDate.toUTCString() + "; path=/";
}
When question 1 would be answered with answer a, the value of the cookie can be adjusted calling a function
that retrieves the actual cookie value and appends a key/value-pair for the current answer separated by "&":
function changeQuizCookie(answer) {
var now = new Date(),
expireDate = new Date(),
quizCookieVal = getQuizCookieValue(),
newQuizCookieVal = quizCookieVal + "&" + answer;
expireDate.setTime(now.getTime() + (10 * 365 * 24 * 60 * 60 * 1000));
document.cookie = "_quizCookie=" + newQuizCookieVal + "; expires=" + expireDate.toUTCString() + "; path=/";
}
So for the example when question 1 would be answered with a, call changeQuizCookie("1=a")
. The cookie value would then
be start&1=a
. I just set "start" as initial cookie value which shouldn't be necessary. I haven't checked if the suggested approach is working (but
think it should), but it's possible just to set the cookie without any value when it's set initally and, when it's value
is changed, to check if quizCookieVal
is empty; if it's empty, newQuizCookieVal = answer
; if not,
newQuizCookieVal = quizCookieVal + "&" + answer
.
And to get the current cookie value:
function getQuizCookieValue() {
var name = "_quizCookie=",
ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) != -1) {
return c.substring(name.length, c.length);
}
}
return "";
}
This is all just as example for how to handle the required functionality using a cookie, as you asked if cookies could be used mentioning that the variable
would change for each question. So this shouldn't be a problem by just retrieving the current cookie value and overwriting it by keeping the previous
cookie value and just appending the current question/answer key/value. Other approach would be to set a single cookie for each question/answer, but just
wanted to give an example for a single-cookie approach.