wifi認證開發過程所用到的,源碼如下:
注:檢測AC是否放行成功,是否可以訪問公網阿裡雲服務器
功能調用:
checkNet().then(function(res) { if(res) { //連網成功 } } else { //連網失敗 } })
功能代碼:
(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(); } })();