3

Not duplicate : I've read many questions like this and it always ended up "use PHP or server-side stuff, and watch out for injection/data manipulation".

I want to store simple stuff on the client side (save and load), like a Google Map location, and want it to stay between refresh of the page.

I don't want to use PHP or any server-side thing.

How can I proceed ?

Thanks

Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261

8 Answers8

4

You can use cookies or localStorage.

Hendrik
  • 413
  • 7
  • 17
Zirak
  • 38,920
  • 13
  • 81
  • 92
2

There are multiple options to store data in client side - IndexedDB, localstorage, webSQL, SessionStorage, Cookies, etc.

IndexedDB

  • Data can be queried efficiently. No limitation in size( but volume or disk drivers limits the size )
  • It will store in Key-Object format
  • It will not be supported in safari browser
  • Support Queries
  • Asynchronous

localstorage

  • It will store value in key-value format (value should be always String)
  • Synchronous
  • Helpful if you need to store a small amount of data
  • Limited size (Depends on browser)

Session Storage

  • If the user closes the tab, it will clear the data

You can check YDN-DB here

Julian Lannigan
  • 6,492
  • 3
  • 18
  • 12
srinath
  • 54
  • 4
2

If html5 is not a problem I would say localstorage is the way to go:


//set value
 localStorage.setItem('todoData', this.innerHTML);

//read value if ( localStorage.getItem('todoData') ) { edit.innerHTML = localStorage.getItem('todoData'); }

ripped from http://net.tutsplus.com/tutorials/html-css-techniques/quick-tip-learning-about-html5-local-storage/ :-)

Ekim
  • 1,105
  • 11
  • 28
1

The key issue you have to keep in mind is you can't trust the client. If it's okay for the client to ask for any location, then it's okay for you to store the location on the client side. But you can't confirm that the value that you get back from the client side is one you have given to that client.

That's what it meant by "data manipulation" [injection is a special type of data manipulation, in that it is manipulated to include things like end quote marks if you're using it as part of a SQL query or other script.]

Random832
  • 37,415
  • 3
  • 44
  • 63
  • Of course, but I never get the location back ! That's just a static webpage... They can do whatever the heck they want with the script and its variable. – Matthieu Napoli Apr 21 '11 at 17:12
  • I agree with you Matthieu, the data you want to store is fine to store. Im not sure why people get so up tight about storing data locally. It really doesnt matter unless its like a password or bank pin or 5MBs+ (due to technical limitations at the moment). Whats the worst that could happen anyways? – Oscar Godson Apr 21 '11 at 17:16
1

I highly suggest using localStorage for a few reasons:

  1. It's supported by modern browsers, INCLUDING IE.
  2. You can store up to 5MB of data (10 in IE) where as a cookie is mere 4KBs
  3. There's lots of libraries to make this easy. One of the most popular is LawnChair: http://westcoastlogic.com/lawnchair/ This will actually write to multiple places, including cookies, so that data isn't lost easily.

Also, as a note, you can't store objects with localStorage, just like you cant with cookies, however you can convert them. For example, if you want to store a Date() don't store it as new Date() store it as: '\'+Date().getTime()+'\'. Same for other objects.

Oscar Godson
  • 31,662
  • 41
  • 121
  • 201
0

Use Cookie.

How to access via javascript.

amit_g
  • 30,880
  • 8
  • 61
  • 118
0

How about storing it in a cookie? For JavaScript I recommend using jQuery, which simplifies a lot of work.

e.g. http://plugins.jquery.com/project/Cookie

Will
  • 2,858
  • 6
  • 33
  • 50
0

Take a look at HTML5 Local Storage

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Imran
  • 87,203
  • 23
  • 98
  • 131