How to check if the user is online using javascript or any library?

前端 未结 10 1718
你的背包
你的背包 2020-12-03 01:35

I need some help on how I could check the internet connection using Javascript or jQuery or any library if available. cause i\'m developing an offline appl

10条回答
  •  春和景丽
    2020-12-03 02:00

    Try utilizing WebRTC , see diafygi/webrtc-ips; in part

    Additionally, these STUN requests are made outside of the normal XMLHttpRequest procedure, so they are not visible in the developer console or able to be blocked by plugins such as AdBlockPlus or Ghostery. This makes these types of requests available for online tracking if an advertiser sets up a STUN server with a wildcard domain.


    modified minimally to log "online" or "offline" at console

    // https://github.com/diafygi/webrtc-ips
    function online(callback){
    
        //compatibility for firefox and chrome
        var RTCPeerConnection = window.RTCPeerConnection
            || window.mozRTCPeerConnection
            || window.webkitRTCPeerConnection;
        var useWebKit = !!window.webkitRTCPeerConnection;
    
        //bypass naive webrtc blocking using an iframe
        if(!RTCPeerConnection) {
            //NOTE: you need to have an iframe in the page
            // right above the script tag
            //
            //
            //
    
                                     
                  
    提交回复
热议问题