HTML5 offline problem: Show a different page when offline

半城伤御伤魂 提交于 2019-12-06 05:57:50

I'm not sure how to do this by manipulating the cache manifest, but you can do it programmatically via JavaScript.

function errorCache(event) {
    // Either a download error occurred or the user is offline
    var offlineURL = 'http://myurl.com/?offline=true'
    window.location = offlineURL;
}

window.applicationCache.addEventListener("error", errorCache, false);

Also you have both / and /Home/Details/2 in your NETWORK and FALLBACK sections. This might be causing unnecessary grief.

For whoever wants to try this using the offline cache: Don't. I think it's not possible. The solution posed in the other answer didn't work for me. Eventually, I've implemented the method from the following blog post: http://ednortonengineeringsociety.blogspot.com/2010/10/detecting-offline-status-in-html-5.html .

For me, this works like a charm.

If you're not convinced that it can't be done with the HTML5 offline cache; I've been trying to following minimal example:

/cache.manifest:

CACHE MANIFEST

FALLBACK:
/html5/content.html /html5/offline.html

NETWORK:
/html5/content.html

/html5/index.html

<!DOCTYPE HTML>
<html manifest="/cache.manifest">
<body>
    <a href=/html5/content.html>content</a> 
</body>
</html>

/html5/content.html

<!DOCTYPE HTML>
<html>
<body>
    Online! :)
</body>
</html>

/html5/offline.html

<!DOCTYPE HTML>
<html manifest="/cache.manifest">
<body>
    Offline..   
</body>
</html>

Since the content.html is in the 'NETWORK' section. the fallback will never work.

The other option is to remove the 'NETWORK' section entirely. In this case the fallback works, but whenever the user visits the content.html when online, it will be cached. - When the user goes offline, the online version is still being displayed.

iow: It seems HTML5 offline cache is not suitable to differentiate between a user being online or offline.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!