Save entire files using localStorage
I would like to use localStorage
to 'cache' some JS and CSS files in a web app that is primarily used on mobile devices (i.e., bandwidth restrictions). I'm thinking it would work something like this:
- The page loads with a small
setup.js
containing code that checkslocalStorage
to see ifbigScriptFile.js
has been stored previously.- If it's not stored,
bigScriptFile.js
is downloaded and stored for the next visit. - If it has been stored, then
bigScriptFiles.js
is read fromlocalStorage
and loaded/run as if it was downloaded like a normal file (i.e.,<script src="http://example.com/bigScriptFile.js"></script>
)
- If it's not stored,
What I'm not sure how to do is step 1.1 -- storing the JS file. (I know that I can only store strings in localStorage
and I know how to use JSON.stringify()
.)
Surely it's not as simple as using escape():
localStorage.setItem('bigScriptFile', escape('myJScode'))
to store, and
unescape(localStorage.getItem['bigScriptFile'])
when retrieving
Even if it is that easy, how do I use JS to get the contents of bigScriptFile.js as开发者_如何学Python a string? Perhaps by making an ajax call to a PHP script that returns the contents?
You should rather put the correct cache headers on js files or look at the HTML5 cache manifest.
W3C specification.
Have you seen http://addyosmani.github.com/basket.js/ might work for what you intended to do
精彩评论