JSON Persistence / DOM
I need to develop an ajax application (using navigation history by anchor) to send requests (GET Method) and receive the answer in JSON. How I can persist the data received in json? , It would be serialized t开发者_JAVA百科o a session or save the client side?. I need to optimize the reload (performance) as a twitter when navigating in the profile and home. Html or json is stored. My question is, whether twitter save the html loaded in the DOM or render data from a json again.
Basically the idea is that, to simulate what makes twitter in the navigability of your website.
Very thanks and sorry for my english !
Take a look at AmplifyJS, which is an HTML 5 polyfill for the localStorage interface. This should let you store your JSON in the browser.
In general, Modernizr has a list of polyfills that may be of interest to you if you're interested in in-browser storage.
I think what you are asking about is a problem that I also faced. When you receive data in JSON format and you use it to construct elements in the DOM, those elements do not persist under these circumstances:
1) when you navigate away from the page
and
2) when you use the back button
What I found tends to occur is that the elements that you created from the JSON data are "lost" and no longer appear when using the Back Button.
Although I can't be sure (I did not have the budget to work out why), I think this is because the Back button sometimes takes your historical page from the cache, and does not request it again. Only the elements that were delivered in the original request are cached, ie not your "JSON created" elements. I'm sure someone can correct me on this!
That being said the only way I found to resolve this is as follows:
- When you page is loaded, execute a function which gets your JSON data using AJAX and builds the rest of the page.
- Just before you build the AJAX request, check to see if the SessionStorage is filled with data. (not Local Storage)
- If it is not then, after you've received the JSON data from the ajax request, store the javascript object in session storage.
- If it is filled use this session storage data instead of making the ajax request.
The obvious benefit of this is that there are less AJAX requests = less network time which is something you mentioned.
精彩评论