Save all files with localStorage

I would like to use localStoragesome JS and CSS files to “cache” in a web application that is mostly used on mobile devices (i.e. bandwidth limits). I think this will work something like this:

  • The page is loaded with a small setup.jscode that checks localStorageto see if it was previously saved bigScriptFile.js.
    • If it is not saved, it is bigScriptFile.jsdownloaded and saved for the next visit.
    • If it was saved, then it is bigScriptFiles.jsread from localStorageand loaded / launched, as if it was loaded as a regular file (i.e. <script src="http://example.com/bigScriptFile.js"></script>)

What I'm not sure how to do this is step 1.1 - saving the JS file. (I know that I can store strings in localStorage, and I know how to use JSON.stringify().)

Of course, this is not as simple as using escape ():
localStorage.setItem('bigScriptFile', escape('myJScode'))for storage, and
unescape(localStorage.getItem['bigScriptFile'])when retrieving

Even if it is so simple, how can I use JS to get the contents of bigScriptFile.js as a string? Perhaps making an ajax call to a PHP script that returns the content?

+5
source share
2 answers

You should rather place the correct cache headers in js files or view the HTML5 cache manifest .

W3C specification .

+11
source

All Articles