Home >Web Front-end >JS Tutorial >JavaScript detects whether the public network server function code can be accessed

JavaScript detects whether the public network server function code can be accessed

陈政宽~
陈政宽~Original
2017-06-28 14:33:071659browse

Check whether the AC is released successfully and whether the public network Alibaba Cloud server can be accessed. Let me share with you the JS function code for detecting whether the public network server can be accessed. Friends who need it can refer to it

It is used in the wifi authentication development process. The source code is as follows:

Note: Check whether the AC is released successfully and whether the public network Alibaba Cloud server can be accessed.

Function call:

checkNet().then(function(res) {
  if(res) {
    //连网成功    
  }
  } else {
    //连网失败        
  }
})


Function code:


(function() {
  //检测入口
  checkNet = function() {
    var def = $.Deferred();
    xiuNet(null).then(function(res) {
      netWork(null).then(function(res) {
        if(res) {
          def.resolve(true);
        } else {
          def.resolve(false);
        }
      })
    })
    return def.promise();
  }
  //嗅探网络,解决ios弹框接入网络问题
  var checkTime = 2; //嗅探次数
  xiuNet = function(def) {
    if(def == null) {def = $.Deferred();}
    var iframe = document.createElement("iframe");
    iframe.style.cssText = "display:none;width:0px;height:0px;";
    document.body.appendChild(iframe);
    var ua = navigator.userAgent;
    if(ua.indexOf("iPhone") != -1 || ua.indexOf("iPod") != -1 ||
      ua.indexOf("iPad") != -1) {
      iframe.src = "http://www.baidu.com" + "?timestap=" + new Date().getTime();
      console.info("嗅探网络----");
      if(checkTime > 1) {
        checkTime--;
        setTimeout(function() {
          xiuNet(def)
        }, 1000);
      } else {
        def.resolve(true);
      }
    } else {
      def.resolve(false);
    }
    return def.promise();
  }
  //检测网络,是否可以连接公网
  var cnetTime = 3; //检测网络次数
  netWork = function(def) {
    if(def == null) {def = $.Deferred();}
    $.ajax({
      type: "get",
      timeout: 2000,
      dataType: "jsonp",
      jsonp: "callback",
      url: 'https://jktv.tv/shphone/ver.txt',
      success: function(data) {},
      error: function(xhr) {
        $("#ddd").html("status:"+xhr.status)
        if(xhr.readyState == 4 && xhr.status == 200) {
          def.resolve(true);
        } else {
          if(cnetTime > 1) {
            cnetTime--;
            setTimeout(function() {
              netWork(def)
            }, 200);
          } else {
            def.resolve(false);
          }
        }
      }
    });
    return def.promise();
  }
})();


The above is the JS test introduced by the editor to see whether the public network server can be accessed. I hope it will be helpful to everyone. If you have any questions If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for your support of the Script House website!

The above is the detailed content of JavaScript detects whether the public network server function code can be accessed. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn