如下所示:
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <meta name="keywords" content="js判斷URL是否可訪問" /> <title>js判斷URL是否可訪問</title> </head> <body> <div>檢驗的url地址:</div> <input type="text" style="width:600px;height:30px;font-size:14px;" id="urlText" value="https://www.baidu.com/" /> <input type="button" value="判斷是否可訪問" onclick="getURL()" /> <br /> <div id="msg1"></div> <div id="msg"></div> <script type="text/javascript" src="js/jquery-1.10.2.js"></script> <script type="text/javascript"> function getURL() { $("#msg").html(""); var url = $("#urlText").val();//請求的url var dateTime = disptime(); var time2 = dateTime.DateTime; $("#msg1").html("發送時間:" + time2); $.ajax({ type: 'get', url: url, cache: false, dataType: "jsonp", //跨域采用jsonp方式 processData: false, timeout:10000, //超時時間,毫秒 complete: function (data) { var dateTime2 = disptime(); var time22 = dateTime2.DateTime; var htmlTxt =[]; if (data.status==200) { htmlTxt.push("成功<br/>"); } else { htmlTxt.push("失敗<br/>"); } htmlTxt.push("readyState=" + data.readyState + "<br/>status=" + data.status + "<br/>statusText=" + data.statusText + "<br/>響應時間:" + time22); var htmlString = htmlTxt.join(''); $("#msg").html(htmlString); } }); } function disptime() { var date = new Date(); var yyyy = date.getFullYear();//四位年份 var month = date.getMonth() + 1;//月份 0-11 var day = date.getDate();//日 var HH = date.getHours();//時 var minute = date.getMinutes();//分鐘 var second = date.getSeconds();//秒 var milliseconds=date.getMilliseconds();//毫秒 if (month < 10) { month = "0" + month; } if (day < 10) { day = "0" + day; } if (HH < 10) { HH = "0" + HH; } if (minute < 10) { minute = "0" + minute; } if (second < 10) { second = "0" + second; } var time = yyyy + "-" + month + "-" + day + " " + HH + ":" + minute + ":" + second + " " + milliseconds; var timeTxt = yyyy + month + day + HH + minute + second; var time = { DateTime: time, TimeTxt: timeTxt } return time; } </script> </body> </html>
以上這篇js判斷請求的url是否可訪問,支持跨域判斷的實現方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持。