Rich
Rich

Reputation: 1779

Javascript cache

I need to get a large amount of data back from a server via ajax. It will take a bit of time to get to the browser. The second time the user goes back to same web page I don't want them to have to download the data via ajax again. Is there anyway Javascript can write the json data to the browsers cache or filesystem reliably?

Cheers

Rich

Upvotes: 2

Views: 784

Answers (5)

Ohad Kravchick
Ohad Kravchick

Reputation: 1154

You can automatically store the AJAX response into localStorage and use either the network reply or the cached version, as explained here: http://myok12.wordpress.com/2011/08/19/building-an-almighty-data-retrieval-system-for-all-html5-webapps/

Upvotes: 0

NullUserException
NullUserException

Reputation: 85458

That's when you can take advantage of having a RESTful service.

If your requests are made via GET and are idempotent (eg: same queried URL will always yield the same response), the browser will cache the response.

See: http://ajaxpatterns.org/RESTful_Service

Upvotes: 3

user8710
user8710

Reputation:

I think if the response has the proper headers, the browser will cache it like any other page. I found this tutorial very informative.

http://www.mnot.net/cache_docs/

Upvotes: 1

ashicus
ashicus

Reputation: 1252

As Tim says, localStorage is a good option, however not all browsers support it. As a fallback, you could store the JSON text in the session.

Upvotes: 2

Tim
Tim

Reputation: 1116

Have you looked at the localStorage spec?

Upvotes: 2

Related Questions