5

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.

Manny_G
  • 333
  • 3
  • 11

3 Answers3

2

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

smistry
  • 1,127
  • 1
  • 9
  • 9
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
Drew Dahlman
  • 4,952
  • 1
  • 22
  • 34
  • 1
    A thing to note is localStorage stores everything as a string so if you intend to save arrays or objects you will need to JSON.stringify them first and then JSON.parse them to get them back to an array/object – Patrick Evans Apr 07 '14 at 19:23
  • This is a good point - and like I said this is a very basic example. Also depending on how you would want to store the data stringify would be required in order to store the data as JSON. – Drew Dahlman Apr 07 '14 at 19:25
  • Also `score = score + 1` should be `score = +score+1` otherwise it will concatenate the 1 to the returned string :) – Patrick Evans Apr 07 '14 at 19:26
  • 1
    @PatrickEvans haha totally - just updated my answer. to make sure to convert the string to an integer – Drew Dahlman Apr 07 '14 at 19:27
  • thanks for responding, but what if I wanted to see the data myself and not have it stored on the users machine? Will I definitely need some server-side code to do this? – Manny_G Apr 08 '14 at 02:03
  • @Manny_G yes you will need server side to do that. – Drew Dahlman Apr 08 '14 at 16:28
0

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.

display name
  • 4,165
  • 2
  • 27
  • 52
  • 2
    I'd recommend [MDN](https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage#localStorage) over [w3schools](http://www.w3fools.com/) which often has outright incorrect information. I've found MDN to be reliable. – Stephen P Apr 07 '14 at 19:30