window.applicationCache.addEventListener(‘updateready’, function(e) {
if (window.applicationCache.status == window.applicationCache.UPDATEREADY) {
// Browser downloaded a new app cache.
// Swap it in and reload the page to get the new hotness.
window.applicationCache.swapCache();
if (confirm(‘A new version of this site is available. Load it?’)) {
window.location.reload();
}
} else {
// Manifest didn’t changed. Nothing new to server.
}
}, false);
}, false);
APPCACHE事件(APPCACHE EVENTS)
也许你已经想到了,还有更多事件可以反映出cache的状态。在诸如下载、app cache更新、出现错误等事件都会让浏览器触发相应事件。下面的代码片段为每一类cache event都设置了监听器:
function handleCacheEvent(e) {
//…
}
function handleCacheError(e) {
alert(‘Error: Cache failed to update!’);
};
// Fired after the first cache of the manifest.
appCache.addEventListener(‘cached’, handleCacheEvent, false);
// Checking for an update. Always the first event fired in the sequence.
appCache.addEventListener(‘checking’, handleCacheEvent, false);
// An update was found. The browser is fetching resources.
appCache.addEventListener(‘downloading’, handleCacheEvent, false);
// The manifest returns 404 or 410, the download failed,
// or the manifest changed while the download was in progress.
appCache.addEventListener(‘error’, handleCacheError, false);
// Fired after the first download of the manifest.
appCache.addEventListener(‘noupdate’, handleCacheEvent, false);
// Fired if the manifest file returns a 404 or 410.
// This results in the application cache being deleted.
appCache.addEventListener(‘obsolete’, handleCacheEvent, false);
// Fired for each resource listed in the manifest as it is being fetched.
appCache.addEventListener(‘progress’, handleCacheEvent, false);
// Fired when the manifest resources have been newly redownloaded.
appCache.addEventListener(‘updateready’, handleCacheEvent, false);
如果manifest文件或者该文件中指定的某个资源下载失败,那么整个更新都会失败。在这种情况下,浏览器会继续试用老的application cache。