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

8 Answers 8

4

You can use cookies or localStorage.

Sign up to request clarification or add additional context in comments.

1 Comment

I used localStorage as a jquery plugin : github.com/medialize/jQuery-store. Thanks
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/ :-)

Comments

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

Comments

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.]

2 Comments

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.
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?
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.

Comments

0

Use Cookie.

How to access via javascript.

Comments

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

Comments

0

Take a look at HTML5 Local Storage

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.