js 判断指定的url是否有效 不能跨域
2016-09-27 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
<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>
<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>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |