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~~
为情所困2017-07-05 10:53:37
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');
};
習慣沉默2017-07-05 10:53:37
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
大家讲道理2017-07-05 10:53:37
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.
欧阳克2017-07-05 10:53:37
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
怪我咯2017-07-05 10:53:37
The script cannot be loaded when the Internet is not available. How to judge? You got the wrong relationship