没有互联网时显示错误页面(html) [英] Show error page when no internet (html)

查看:100
本文介绍了没有互联网时显示错误页面(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中的offlineonline事件.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆