javascript - js determines whether the user's network can access the Internet?
女神的闺蜜爱上我
女神的闺蜜爱上我 2017-07-05 10:51:37
0
9
1057

How does the front end determine whether the user's network can access the Internet? Because some users cannot have an internal network without an external network, so if they want to detect those who cannot access the Internet, they can cancel the connection to the external link. Can anyone with experience give me a way to judge~~~
Demo~~

女神的闺蜜爱上我
女神的闺蜜爱上我

reply all(9)
为情所困

Just find a picture, such as the baidu logo below, and test whether the picture can be opened

var i = new Image();
i.src = 'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo_top_ca79a146.png?t=' + Date.parse(new Date());
i.onload = function() {
    alert('ok');
};
i.onerror = function() {
    alert('fail');
};
淡淡烟草味

Request Baidu. If successful, it means there is an external network.

習慣沉默

You can write an onerror directly in the script, <script src="1.js" onerror="method (process in this method...)"></script> If it cannot be loaded, it means there is a problem with the network

大家讲道理

https://github.com/alfg/ping....
It is directly packaged and can be used as soon as you get it. The author of the question will divide and post it.

仅有的幸福

navigator.onLine, detect network connection

迷茫

Just pull Baidu homepage with ajax

仅有的幸福

window.navigator.onLine

https://developer.mozilla.org...

学霸

Before asking questions, you should make good use of search engines. You can directly search for "JavaScript Network" on sf to get the results you want.

js determines whether the network is disconnected

刘奇

The script cannot be loaded when the Internet is not available. How to judge? You got the wrong relationship

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template