没有互联网时显示错误页面(html) [英] Show error page when no internet (html)
问题描述
我已经创建了一个网站,想知道当没有互联网连接时如何显示自定义错误页面.例如,
I have created a website and wanted to know how I can show a custom error page when there is no internet connection. For example,
推荐答案
您可以使用 Java中的offline
和online
事件.
You can use the offline
and online
events in Javascript.
这是一个简单的代码段,可以在发生这些事件时提醒用户,但是您可以扩展该想法以提供更好的UI.
Here's a simple snippet that alerts the user on these events, but you can extend the idea to present a better UI.
window.addEventListener("online", function() {
alert("You are online now!");
});
window.addEventListener("offline", function() {
alert("Oops! You are offline now!");
});
此外,请注意,您可以使用navigator.onLine
Also, note that you can check whether the page is online or not using navigator.onLine
if (navigator.onLine) {
console.log("You are online");
} else {
console.log("You are offline");
}
注意:您可以通过网络"标签在Chrome中进行测试,可以选择使该标签暂时脱机.
Note: You can test these in Chrome through Network tab, where there is an option to take the tab offline temporarily.
这篇关于没有互联网时显示错误页面(html)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!