<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="判断是否可访问" οnclick="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>
阅读排行
- Java面试题全集(上) (1101770 )
- Wi-Fi 爆重大安全漏洞,Android、iOS、Windows 等所有无线设备都不安全了 (422201 )
- Jquery 使用Ajax获取后台返回的Json数据后,页面处理 (267443 )
- Java面试题全集(中) (235781 )
- 一个非常有用的函数——COALESCE (221896 )
- Java面试题全集(下) (219786 )
- Uncaught SyntaxError: Unexpected token ) (212406 )
- 如何用adb连接android手机?(我的亲自经历)------ 顺便说说unable to connect to 192.168.1.100:5555的原因和解决方法 (209595 )
- 如何利用C/C++逐行读取txt文件中的字符串(可以顺便实现文本文件的复制) (206495 )
- yum提示Another app is currently holding the yum lock; waiting for it to exit... (204577 )