counting result in javascript
counting result in javascript
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 ?
Answer by a?r? for counting result in javascript
localStorage.setItem("count",count);
then you can call it from different pages with
var count= localStorage.getItem("count");
Answer by Neil for counting result in javascript
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); }
Answer by Viktor Kukurba for counting result in javascript
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") }
Answer by Pamblam for counting result in javascript
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
Answer by Dropout for counting result in javascript
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
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"} ]
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment