1

在我的聊天应用程序中,当 Internet 连接处于非活动状态时,我需要给用户一个提示?

所以我使用了 JavaScript navigator.onLine,但它适用于 IE 和 Chrome。不在其他浏览器中。

我的 JavaScript 将是:

function pingAction(){
    if (navigator.onLine) {
        setTime();
        try
        {
            xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    var checkMsg = xmlhttp.responseText.toString();

                
                    if(checkMsg != "null" && checkMsg.length != 0  && checkMsg != null){

                        var stringToArray = new Array;
                        stringToArray = checkMsg.split("<//br//>");
                        var len = stringToArray.length;
                        for(var i=0;i<len-1;i++){
                            //alert(stringToArray[i]);
                            getText(stringToArray[i]);

                        }

                    }
                }
            };

            xmlhttp.open("POST","PingAction",true);
            xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            xmlhttp.setRequestHeader("charset","UTF-8");
            xmlhttp.send("userId="+encodeURIComponent(userId)+"&secureKey="+encodeURIComponent(secureKey)+"&sid="+Math.random());
        }
        catch(err)
        {
            alert(err.description);
        }
    }else{
        //alert("offLine   ...");
        document.getElementById("serverMsg").style.backgroundColor="yellow";
        document.getElementById("serverMsg").innerHTML ="Your Network connection is failed !";
        document.getElementById("serverMsg").style.display="inline";
    }
}

希望各位大侠给个好的解决方案。

4

0 回答 0