1

How can i access the data included in html5, like this in header:

<script type="application/json" src="data.json">

Many people suggest using $.get("data.json"), however, that forces to load the data everytime. And the data is huge, and doesn't change often. It's a phonebook. So the point is to use that file from the browser's cache.

any ideas? is it posible?

6
  • The cache can be cleaned by the browser or by external programs, so it's far from foolproof. You should store it in an html 5 browser side database instead. Commented Jul 15, 2014 at 20:36
  • @EngineerDollery Why would you ever do that? The browser already knows how to cache stuff, why would you go through all the trouble to re-invent that? Besides, the browser can also clear local storage. Commented Jul 15, 2014 at 20:39
  • To be sure of your caching is working, you can apply a cache-control using the 304 - Not Modified header. This header is controlled by your server, so is more reliable than letting the browser to control it. But for that, you will have to use a back-end response to control the headers, instead of requesting a file directly. If you're interested, I could post an answer of it. Commented Jul 15, 2014 at 21:12
  • @Brad -- because the user can disable caching and can clear caching. Other external programs, such as McAfee and Norton on Windows can also clear browser caches as part of disk cleanup. If you rely on caching then you're much more likely to have users download the same data repeatedly. Local storage was invented to circumvent most of these issues, and yes, it can be cleared, but usually only by the browser itself and it takes an explicit action, unlike caching. Commented Jul 15, 2014 at 21:42
  • @EngineerDollery that's true. But it's ok, to reload, the point is to avoid loading every time. The disabled cache is the worst case, but uncommon (the whole internet becomes very slow without caching). I think relying on html5 and browser cache technology (like etags) is the most efficient and standard/compatible way. You can tell the browsers when to expire the json data, if there's a new version of it. Commented Jul 15, 2014 at 22:22

2 Answers 2

3

Many people suggest using $.get("data.json"), however, that forces to load the data everytime.

That's not true. Assuming you've got caching set up properly, the browser can still cache the results of an AJAX get. (In fact, that's the reason for a lot of questions on StackOverflow, where people keep getting the same results from their AJAX request, and are wondering why.)

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

Comments

0

Set up cache-control in response header for caching this file. http://www.mobify.com/blog/beginners-guide-to-http-cache-headers/

1 Comment

Welcome to Stack Overflow. Can you provide an example in your answer, rather than just a link? Link-only answers are frowned upon here.

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.