How do i keep score in html game using javascript

i have made a simple game using html ,css and java ,now i need to keep scores ,so each
time the person gets the correct answer it adds +1 ,but i have no idea how to do it ,my game
consists of pages and each page has its own question and by using math random () i have made it to randomize the pages ,but how do i keep scores so each page has a score display which adds +1 on each page as the person keeps getting correct answer

the link to my code of the first page is here https://www.mediafire.com/?hl54q2gq88u8yc2 ,please tell me how can i do it ,i searched all over the net but i have not found a solution

NOTE: if you see my code , the answer to my first question is WHITE ,so how do i add a score to that so it keeps adding +1

You can use these cookie handling functions to save and read information that will be maintained between pages.

function createCookie(name, value, days) {
    var expires = '',
        date = new Date();
    if (days) {
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = '; expires=' + date.toGMTString();
    }
    document.cookie = name + '=' + value + expires + '; path=/';
}
 
function readCookie(name) {
    var nameEQ = name + '=',
        allCookies = document.cookie.split(';'),
        i,
        cookie;
    for (i = 0; i < allCookies.length; i += 1) {
        cookie = allCookies[i];
        while (cookie.charAt(0) === ' ') {
            cookie = cookie.substring(1, cookie.length);
        }
        if (cookie.indexOf(nameEQ) === 0) {
            return cookie.substring(nameEQ.length, cookie.length);
        }
    }
    return null;
}
 
function eraseCookie(name) {
    createCookie(name, '', -1);
}

Another option is Web Storage API

Yes, that’s a good solution these days too.

How well supported is that these days? It seems considerably simpler than messing around with cookies.

Edit: Ignore me - browser compatibility was a further down the page than I’d read.

It is much simpler, there’s good polyfills available too.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.