search

Home  >  Q&A  >  body text

javascript - js determines whether the user's network can access the Internet?

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~~

女神的闺蜜爱上我女神的闺蜜爱上我2726 days ago1064

reply all(9)I'll reply

  • 为情所困

    为情所困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');
    };

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-07-05 10:53:37

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

    reply
    0
  • 習慣沉默

    習慣沉默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

    reply
    0
  • 大家讲道理

    大家讲道理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.

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-07-05 10:53:37

    navigator.onLine, detect network connection

    reply
    0
  • 迷茫

    迷茫2017-07-05 10:53:37

    Just pull Baidu homepage with ajax

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-07-05 10:53:37

    window.navigator.onLine

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

    reply
    0
  • 欧阳克

    欧阳克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

    reply
    0
  • 怪我咯

    怪我咯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

    reply
    0
  • Cancelreply