javascript 通过javascript清除和更新html5应用程序缓存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11817578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:26:34  来源:igfitidea点击:

Purge and update html5 application cache through javascript

javascripthtmlapplication-cache

提问by tusharmath

I arrive to this problem quite a lot of times, where some of the users have a corrupt application cache (HTML 5).

我多次遇到这个问题,其中一些用户的应用程序缓存(HTML 5)损坏。

I do update the manifest file every time there is a new release still some times some users get a corrupt application cache.

每次有新版本时,我都会更新清单文件,但有时某些用户会得到损坏的应用程序缓存。

I such a case I want to fully clear what is there in their application cache and load all the fresh content from the server.

在这种情况下,我想完全清除他们的应用程序缓存中的内容并从服务器加载所有新鲜内容。

Is there a way to that using Javascript?

有没有办法使用Javascript?

回答by Rob Angelier

According to the following article on

根据以下文章

http://www.w3schools.com/html5/html5_app_cache.asp

http://www.w3schools.com/html5/html5_app_cache.asp

there are three ways on wich the application cache will be reset, these are:

有三种方法可以重置应用程序缓存,它们是:

  1. The user clears the browser cache
  2. The manifest file is modified
  3. The application cache is programmatically updated
  1. 用户清除浏览器缓存
  2. 清单文件被修改
  3. 应用程序缓存以编程方式更新

More information about programmatically updating the application cache can be found here:

可以在此处找到有关以编程方式更新应用程序缓存的更多信息:

http://www.html5rocks.com/en/tutorials/appcache/beginner/

http://www.html5rocks.com/en/tutorials/appcache/beginner/

It looks something like this:

它看起来像这样:

var appCache = window.applicationCache;

appCache.update(); //this will attempt to update the users cache and changes the application cache status to 'UPDATEREADY'.

if (appCache.status == window.applicationCache.UPDATEREADY) {
  appCache.swapCache(); //replaces the old cache with the new one.
}

回答by Pedro Cardoso

This one is quite old but as I see a wrong answer being up-voted, I felt like giving some hint....

这个已经很老了,但是当我看到一个错误的答案被投票时,我想给出一些提示......

If ones has the trouble of looking at the spec, you can see that there's no way for code to force the browser to reload the cache, unless there's a change in the manifest, and that's when "appCache.status == window.applicationCache.UPDATEREADY" is true.

如果您在查看规范时遇到麻烦,您会发现代码无法强制浏览器重新加载缓存,除非清单中发生更改,这就是“appCache.status == window.applicationCache.js”。 UPDATEREADY”是真的。

Look here http://www.w3.org/TR/2011/WD-html5-20110525/offline.html

看这里http://www.w3.org/TR/2011/WD-html5-20110525/offline.html

"updateready The resources listed in the manifest have been newly redownloaded, and the script can use swapCache() to switch to the new cache."

“updateready 清单中列出的资源已经重新下载,脚本可以使用 swapCache() 切换到新缓存。”

So, reading it carefully, you find that the applicationCache gets to that status when the resources where just downloaded... that is.. a previous "downloading" event occurred... and previous to that one a "checking"....

所以,仔细阅读它,你会发现 applicationCache 在刚刚下载的资源时进入那个状态......也就是说......发生了先前的“下载”事件......并且在该事件之前发生了“检查”...... .