Yes, you can. (sorry for answering my own question, I thought this was an interesting solution)
I found a sample code diagram here on slide # 12.
http://www.slideshare.net/jedisct1/abusing-javascript-to-speedup-mobile-web-sites
I implemented this at http://m.bbref.com/ (still in beta)
You need to use the script url version to clear the cache when creating the new version, but this works for pages with localStorage and will also work if localStorage is not available. I have added debugging code in the footer to show you where js is loading from.
I split it into a script for the header and one of the footer. They appear on the line.
In the header (I added it here when we use modernizr to add some classes to the html tag, and I want them there as quickly as possible for rendering purposes. It can be moved to the footer.
<script type="text/javascript"> // .001 is the current js version // this script assumes it is in the root web directory. var js_file = "site_lib.001.js"; // vars to store where the file is loaded from. var _mob_load_js = false; var _mob_ajax_load_js = false; { // if we have localStorage and the files exists there get it. if(window.localStorage && window.localStorage[js_file]) { // eval the js file. try{ window.eval(window.localStorage[js_file]); // successfully eval'ed the code, so // we don't need to download it later. _mob_load_js = true; } catch (e) { _mob_load_js = false; } } else if (window.localStorage) { // We have localStorage, but no cached file, so we // load the file via ajax, eval it and then store // the file in localStorage // To remove previous versions, I remove all of our localStorage, // This is extreme if we store other vars there. window.localStorage.clear(); // standard ajax request. var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState == 4) { // eval the js try { window.eval(xhr.responseText); // successfully eval'ed the code, so // we don't need to download it later. _mob_ajax_load_js = true; } catch (e) { _mob_ajax_load_js = false; } try { // store the js. window.localStorage[js_file] = xhr.responseText; } catch (e) {} } else { return; } }; xhr.open("GET",js_file,true); xhr.send(); } }; </script>
and in the footer (for performance reasons). I put the standard boot method. Note that browsers that use this branch will cache completely until you run out of headers.
<div id="sr_external_script"></div> <script type="text/javascript"> </script>
I confirmed in chrome and safari that js is loading from localStorage, and the functionality of the site works as expected, and the server does not receive a request. And I confirmed that when working in IE or firefox, it loads the script as shown in the footer.
Note. I added code to wrap evals in a try catch since I am having a problem with firefox.