-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathindex.html
50 lines (46 loc) · 2.17 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<!doctype html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<script type="text/javascript">
var t1 = new Date();
/*
* cached-webpgr.js - simple localStorage based caching of JavaScript files
* https://github.com/webpgr/cached-webpgr.js
* Author: Webpgr http://webpgr.com by Falko Krause <falko@webpgr.com>
* License: MIT
*/
function _cacheScript(c,d,e){var a=new XMLHttpRequest;a.onreadystatechange=function(){4==a.readyState&&(200==a.status?localStorage.setItem(c,JSON.stringify({content:a.responseText,version:d})):console.warn("error loading "+e))};a.open("GET",e,!0);a.send()}function _loadScript(c,d,e,a){var b=document.createElement("script");b.readyState?b.onreadystatechange=function(){if("loaded"==b.readyState||"complete"==b.readyState)b.onreadystatechange=null,_cacheScript(d,e,c),a&&a()}:b.onload=function(){_cacheScript(d,e,c);a&&a()};b.setAttribute("src",c);document.getElementsByTagName("head")[0].appendChild(b)}function _injectScript(c,d,e,a){var b=document.createElement("script");b.type="text/javascript";c=JSON.parse(c);var f=document.createTextNode(c.content);b.appendChild(f);document.getElementsByTagName("head")[0].appendChild(b);c.version!=e&&localStorage.removeItem(d);a&&a()}function requireScript(c,d,e,a){var b=localStorage.getItem(c);null==b?_loadScript(e,c,d,a):_injectScript(b,c,d,a)};
// ---
requireScript('jquery', '1.11.5', 'http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js', function(){
requireScript('examplejs', '0.0.5', 'example.js');
});
</script>
</head>
<body>
<h1>localStorage Script Caching Demo</h1>
<p>This code demonstrates how to use the localStorage to cache and JavaScrip files. <br>
Cached files will be loaded much faster than scripts from any server (even local ones). <br>
My test showed:
<table>
<tbody>
<tr>
<td>Loading jQuery from CDN</td>
<th>268ms</th>
</tr>
<tr>
<td>Loading jQuery from localStorage</td>
<th>47ms</th>
</tr>
</tbody>
</table>
<br>
<sub>
This demo will only work on a server, file:// is not supported.
</sub>
</p>
</body>
</html>