10

I want to cache the following JSON object in the page so that I can retrieve it on another page.

var test = {
        "lists":["list1","list2","list3","list4","list5"],
        "maps": {  
            "key4":"value4","key3":"value3","key5":"value5","key2":"value2","key1":"value1"
        },
        "number1":123456789,
        "numberarray1":[1,2,3,4,5,6,7,8,9],
        "string1":"A",
        "stringarray1":["A1","B1"]
    }

How do I cache it and retrieve it again?

Is this possible? If not are there other ways that I can save my JSON object and then retrieve it on another page.

Thank you.

4
  • If the "other page" is an entirely different page requireing a page refresh to access it, the only way you can store it is through either cookies or HTML5 storage, both of which are string only storage.
    – Kevin B
    Oct 5, 2012 at 15:25
  • You going to need to do use a cookie or localstorage Oct 5, 2012 at 15:25
  • yes i am talking about the html. so how can i do the cookie saving via jquery? if the data is confidential, is it safe to use?
    – newbie
    Oct 5, 2012 at 15:27
  • can i do the user session using jquery?
    – newbie
    Oct 5, 2012 at 15:27

3 Answers 3

10

save that tasty cookie

$.cookie("test-data", JSON.stringify(test));

get that sweet cookie back

var test =  JSON.parse($.cookie("test-data"));

http://www.electrictoolbox.com/jquery-cookies/

11
  • my problem in using cookie is that it is permanent.. i cannot do this if(test == null) ... then create a cookie else.. use saved cookie... BECAUSE what if the json object is updated.
    – newbie
    Oct 5, 2012 at 15:52
  • Well, that will be an issue no matter how you store it, web database or cookie. Oct 5, 2012 at 15:54
  • Can you give more details about what you are trying to do? What the final product is supposed to be? Oct 5, 2012 at 15:58
  • i received a json object by calling a url but retrieving it is so slow... 2min/retrieval... i need to use that json object in two of my pages... that's why i need to save it in order to retrieve it very fast. so if i reload my page, or if i go to the next page, the loading will be faster. Note: that json object can change... so i cannot store it in a cookie
    – newbie
    Oct 5, 2012 at 16:01
  • that's why i think i need just to cache the data... because it is more likely temporary?
    – newbie
    Oct 5, 2012 at 16:03
4

In addition to the provided answers, in more complex scenarios I would recommend using jStorage plugin.

Description from the website:

jStorage is a cross-browser key-value store database to store data locally in the browser - jStorage supports all major browsers, both in desktop (yes - even Internet Explorer 6) and in mobile. Additionally jStorage is library agnostic, it works well with any other JavaScript library on the same webpage, be it jQuery, Prototype, MooTools or something else. Though you still need to have either a third party library (Prototype, MooTools) or JSON2 on the page to support older IE versions.

jStorage supports storing Strings, Numbers, JavaScript objects, Arrays and even native XML nodes. jStorage also supports setting TTL values for auto expiring stored keys and - best of all - notifying other tabs/windows when a key has been changed or publishing/subscribing to events from the same or another tab/window, which makes jStorage also a local PubSub platform for web applications.

If jStorage is loaded on the page localStorage and sessionStorage polyfills are added to IE6 and IE7 in addition to regular $.jStorage methods. You can use regular setItem/getItem methods with the polyfills but getter/setters can be used as well - localStorage.mykey = myval; is absolutely valid with jStorage. The only downside is that you can't use onstorage event, you need to fall back to listenKeyChange instead.

jStorage is pretty small, about 10kB when minified and 4kB gzipped.

1

Are you talking about caching in HTML? You could always use LocalStorage which will make it available to any pages in the same domain.

If you combine that with Modernizr and cookies, you can end up with some kind of solution.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.