问题
I am just learning JavaScript and web development in general and I was wondering if what I want to do is possible. I would like to write a JavaScript quiz that saves the answers a user inputs without needing a backend. Is this possible?
If it is not possible what is the simplest and easiest way I can do this?
Thank You
PS: If this is not the right place to ask this, please direct me to the place I should be asking this.
回答1:
Yes you can either use cookies
or localstorage
http://developer.mozilla.org/en-US/docs/Web/API/document.cookie
http://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage
回答2:
If you're planning on just storing them for just the user you could use localstorage
There are a number of ways to use this, but this is a very simple version that should point you in the right direction.
As @patick mentions below - if you wanted to store more complete data you would need to JSON.stringify the data then parse later when you are ready to consume the data.
Also note that all localstorage is saved as a string so you would need to make it an integer to really use it.
Potentially you could do
// set inital score
var score = parseFloat(localStorage.getItem('score'));
// if they get the answer correct
score = score + 1;
// update the score
localstorage.setItem('score',score);
// go to next question
回答3:
Try HTML5 Web Storage. With HTML5, web pages can store data locally within the user's browser.
Here is a quick tutorial on MDN
Another tutorial: http://mrbool.com/creating-a-crud-form-with-html5-local-storage-and-json/26719
It is important to note that Internet Explorer 7 and earlier versions, do not support Web Storage.
来源:https://stackoverflow.com/questions/22921085/how-to-save-user-input-without-backend