My webapp is primarily used on mobiles so I'm looking to store static CSS and JS files to reduce the loading time. Its a trick even Google and Bing seems to use I read somewhere.
Although I understand getting and setting values is easy:
// Put the object into storage
localStorage.setItem('testObject', JSON.stringify(testObject));
// Retrieve the object from storage
var retrievedObject = localStorage.getItem('testObject');
... but I'm not sure how I could set, get and render/execute CSS stylesheets and Javascript files. Any example would help.
In your example it should be
var retrievedObject = JSON.parse(localStorage.getItem('testObject'))
;. As localStorage can only handle string value pairs by now, it would be worth a try to make up a new Object like this:Here is an example:
You can fill an array of object like these by your server side implementation and append them to the dom later on. This example only handles CSS and makes no difference for deffered items, as it appends everything to the
<head>
, but you get the idea.I copied the interesting part of this answer: https://stackoverflow.com/a/6211716/3244925
These objects can be saved in the localStorage using:
localStorage.setItem('resources', JSON.stringify(resources));
and later on be re used.Update
I feel the answer to your question may be much simpler. If your .css and .js files are static, you may just make up an array of external resources (urls) and append those urls to the
<head>
and<body>
later on, once you loaded them from thelocalStorage
. This will render this inline styles and javascript useless, and it would be a much cleaner solution.maybe you are looking for appCache which download the source of your files once and checks only if appcache file is modified to update your source.
This is really simple to implement.
update your html
Create your appcache file: myfile.appcache in your project root folder
See official doc for more info : http://www.w3schools.com/html/html5_app_cache.asp
Good explanation from html5rocks : http://www.html5rocks.com/en/tutorials/appcache/beginner/
Once you load a webpage for the first time, browser stores all its major content in the cache. So, since this caching process is not biased you can not make any content stable under browser cache.
HTML5 has a utility called local storage. It is used to save data like cookies and text under a special storage provided by the browser & cache does not have any relation with it.
However, you can store the css in the string format inside localstorage but not the css file.