js检测网址是否可访问,javascript检测网址是否可访问,支持跨域;
<!DOCTYPE html>
<html>
<head><meta name="viewport" content="width=device-width" /><title>url检测是否可访问</title><style></style>
</head>
<body><div id="km" style="font-size:23px;"></div><script type="text/javascript">function getTimeDiff(startTime, endTime) {//var start = Date.parse(startTime);//var end = Date.parse(endTime);//var diff = Math.abs(end - start); // 返回时间差的毫秒数var diff = Math.abs(endTime - startTime); // 返回时间差的毫秒数return diff;//var seconds = Math.floor(diff / 1000);//var minutes = Math.floor(seconds / 60);//var hours = Math.floor(minutes / 60);//var days = Math.floor(hours / 24);// return days + "天 " + (hours % 24) + "小时 " + (minutes % 60) + "分钟 " + (seconds % 60) + "秒";}function importScript(sSrc, fOnload) {document.getElementById("km").innerHTML += "<br/>" + sSrc + "检测中....<br/>";var isConnected = false;var oScript = document.createElement("script");oScript.type = "text/javascript";var startTime = new Date();oScript.onerror = function (r) {var millisecond = getTimeDiff(startTime, new Date());console.log("连接出错-------------- " + sSrc);console.log(444, r);document.getElementById("km").innerHTML += "<br/>" + sSrc + "连接出错,耗时" + millisecond + "毫秒<br/>";};//if (fOnload) {// oScript.onload = fOnload;//}oScript.onload = function (r) {var millisecond = getTimeDiff(startTime, new Date());isConnected = true;console.log("连接成功-------------- " + sSrc);console.log(555, r);document.getElementById("km").innerHTML += "<br/>" + sSrc + "连接成功,耗时" + millisecond + "毫秒<br/>";};document.currentScript.parentNode.insertBefore(oScript,document.currentScript,);oScript.src = sSrc;setTimeout(function () {//连接超时,触发if (!isConnected) {console.log("连接超时-------------- " + sSrc);var millisecond = getTimeDiff(startTime, new Date());document.getElementById("km").innerHTML += "<br/>" + sSrc + "连接出错,超时,耗时" + millisecond + "毫秒<br/>";//oScript.remove();//window.location.href = "http://www.baidu.com";}}, 800);}!function () { importScript("http://km.gg.com.cn:10081", function () { });importScript("http://km.bb.com.cn", function () { });}();</script>
</body>
</html>