0

i'm trying to make a drag and drop quiz, each question is on a different html page and i need to get the result so i made a javascript in an external javascript file it worked on 1 html page but it doesn't on multiple pages, like .. the function is resetting itself. i'm so not pro in programming so here's the counter js.js file

var count=0; 
function fun() {
  count=count+1;

}

function result(){
    document.write("your mark is " + count + " from 9")

}

and here's the code in the other pages .. i made 3 just to test it

 if ( slotNumber == cardNumber ) {
    ui.draggable.addClass( 'correct' );
    ui.draggable.draggable( 'disable' );
    $(this).droppable( 'disable' );
    ui.draggable.position( { of: $(this), my: 'left top', at: 'left top' } );
    ui.draggable.draggable( 'option', 'revert', false );
    correctCards++;
    fun();
  } 

so how can i call it on a different pages without having it reset itself ?

Dee994
  • 55
  • 1
  • 8
  • 1
    Cookie or localstorage. – Andy Feb 24 '16 at 13:14
  • @Andy can you tell me in detail because i didn't understand – Dee994 Feb 24 '16 at 13:15
  • 1
    Possible duplicate of [Passing javascript variables between pages](http://stackoverflow.com/questions/11581543/passing-javascript-variables-between-pages) – JJJ Feb 24 '16 at 13:20
  • Perhaps not use multiple pages after all? – John Dvorak Feb 24 '16 at 13:20
  • @JanDvorak it's a drag and drop quiz and i'm gonna call an audio on each page .. it's gonna be a mess if i put all of it on one page – Dee994 Feb 24 '16 at 13:27
  • @Dee994 is the audio at least optional? And no, audio doesn't mean the content should be split into multiple pages. You can add audio elements dynamically or (better yet) use Web Audio. – John Dvorak Feb 24 '16 at 13:28
  • @JanDvorak i'm gonna add an audio for each question .. it's a spelling quiz the user should put the right answer in the right slot and he can listen to the audio .. the audio spells the word for the user – Dee994 Feb 24 '16 at 13:31

5 Answers5

2
localStorage.setItem("count",count); 

then you can call it from different pages with

var count= localStorage.getItem("count");
John Dvorak
  • 26,799
  • 13
  • 69
  • 83
mr. pc_coder
  • 16,412
  • 3
  • 32
  • 54
1

You can use localStorage to store count value for all pages of same domain.

function fun() {
  var count = localStorage.getItem("count") || 0;
  localStorage.setItem("count", ++count);
}

function result() {
  document.write("your mark is " + (localStorage.getItem("count") || 0) + " from 9")
}
Viktor Kukurba
  • 1,360
  • 9
  • 14
  • sir do you have any idea how can i insert the value of count after using the local storage into the database ? and thank you very much it worked perfectly – Dee994 Mar 05 '16 at 07:23
0

Try this. This will remember count for you for successive page loads (unless your browser does not support it, in which case it will reset to 0 on page load).

function lstorage(key, val) {
    if(typeof(Storage) !== "undefined") {
        if(typeof(val) !== "undefined") {
            localStorage.setItem(key, val); 
        } else {
            return localStorage.getItem(key);
        }
    }
}

var count=lstorage("savedCount") || 0; 
function fun() {
    count++;
    lstorage("savedCount", count);
}
Neil
  • 5,762
  • 24
  • 36
0

Here's an example with cookies.

// Set a cookie
function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
} 

// Get a cookie
function getCookie(cname) {
    var name = cname + "=";
    var 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) == 0) return c.substring(name.length,c.length);
    }
    return "";
} 

Source

I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
0

If the website uses only HTML and JavaScript, then the best choice would probably be to use cookies or local storage.

For example with cookies at the beginning of the test you would create an empty cookie to which the answers would be stored. The format is up to you, but I like to use JSON-like structure for everything, so I'd do the following:

Create a function to get your desired cookie:

function getAnswers(){
    var cookies = document.cookies;
    for(var i=0; i<cookies.length; i++){
        if(cookies[i].split("=")[0] == "answers"){
            return cookies[i].split("=")[1];
        }
    }
    return null;
}

On start of the test:

document.cookie="answers=[]";

On each answer:

var answers = JSON.parse(getAnswers());
answers.push({
    "question": questionNumber, //the id of the question
    "answer": pickedAnswer //what user selects
});
document.cookie("answers=" + JSON.stringify(answers));

In the end upon executing getAnswers() you will end up with an array of objects holding information about every submitted answer. For example

[
    {"1": "A"},
    {"2": "D"},
    {"3": "C"},
    {"4": "B"}
]
Dropout
  • 13,653
  • 10
  • 56
  • 109