This method has been proven to work, mark. (Note: Not the only way!)
The principle is to request a picture on the server. If the request is successful, it is judged that there is a network, and if the request fails, it is judged that there is no network.
Here’s the code. Let me just copy and paste it.
<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, <title>Document</title> </head> <body> </body> <script> var EventUtil = {addHandler: function (element, type, handler) { if (element.addEventListener) { element.addEventListener(type, handler, false); } else if (element.attachEvent) { element.attachEvent("on" + type, handler); } else { element["on" + type] = handler; } }, removeHandler: function (element, type, handler) { if (element.removeEventListener) { element.removeEventListener(type, handler, false); } else if (element.detachEvent) { element.detachEvent("on" + type, handler); } else { element["on" + type] = null; }}}; var dom = document.createElement('img'); dom.style = 'display:none; '; dom.src = 'http://www.baidu.com/img/bdlogo.gif'; dom.id = 'map_img'; Eventutil.addhandler (dom, 'error', function () {alert(' your computer is not connected to the network. '); }); document.body.appendChild(dom); </script> </html>Copy the code