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 ?

7
  • 1
    Cookie or localstorage. Commented Feb 24, 2016 at 13:14
  • @Andy can you tell me in detail because i didn't understand Commented Feb 24, 2016 at 13:15
  • 1
    Possible duplicate of Passing javascript variables between pages Commented Feb 24, 2016 at 13:20
  • Perhaps not use multiple pages after all? Commented Feb 24, 2016 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 Commented Feb 24, 2016 at 13:27

5 Answers 5

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

then you can call it from different pages with

var count= localStorage.getItem("count");
Sign up to request clarification or add additional context in comments.

Comments

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")
}

1 Comment

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
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);
}

2 Comments

Is there still a browser out there with no localStorage support?
@JanDvorak Probably not, but it can't hurt to check, right?
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

Comments

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"}
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.