HTML5技术

HTML教程:初学者该如何使用Application Cache_HTML教程(2)

字号+ 作者:H5之家 来源:H5之家 2015-09-24 18:03 我要评论( )

} 注意:像这样使用 update()和swapCache()并不会将更新后的资源 呈现给用户。这仅仅是让浏览器检查manifest文件是否发生了更新,然后下载指定的更新内容,重新填充app cache。因此,要让用户看到更新后的内容,需

  }

    注意:像这样使用 update()和swapCache()并不会将更新后的资源 呈现给用户。这仅仅是让浏览器检查manifest文件是否发生了更新,然后下载指定的更新内容,重新填充app cache。因此,要让用户看到更新后的内容,需要两次页面下载,一次是更新app cache,一次是更新页面内容;

为了让用户能看到你的站点的最新版本,设置一个监听器来监听页面加载时的updateready 事件;

// Check if a new cache is available on page load.

  window.addEventListener(‘load’, function(e) {

  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.

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
网友点评