jQuery:检测跨域URL网址有效性

使用JQUERY检测外部跨域URL网址有效性,判断外网链接是否能打开或是否有效。

以下代码可以在本地测试也可以在服务器端测试,详细代码如下:

<a id="a1" href="https://www.xxx.com/" target="_blank">网站建设【jquery检测URL有效性】</a>

<script type="text/javascript" src="https://www.xxx.com/js/jquery-2.1.1.min.js"></script>

<script type="text/javascript">
	$("#a1").click(function(){
		$.ajax({
			url: $("#a1").attr("href"),
			type: 'GET',
			cache: false,
			dataType: "jsonp", //跨域采用jsonp方式
			processData: false,
			timeout:10000, //超时时间,毫秒
			complete: function (data) {
				if (data.status==200) {
					alert('有效');
				} else {
					alert('无效');
				}
			}
		});
	});
</script>

执行结果如下图:

posted @ 2025-05-10 08:51  Dy大叔  阅读(34)  评论(0)    收藏  举报