页面地址有效?无效?
html页面:
<!DOCtype HTML> <html> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="/demo/JS/jq.js"></scrIPt> </head> <body> <a href="http://www.bAIdu.com/">【jQuery检测链接有效性】</a> <a href="www.baidu.com/">【jquery检测链接有效性2】</a> //<script type="text/JavaScript" src="js/base.js"></script> </body> </html>
JS页面:
//判断地址有效性 $("body a").each(function(){ $(this).click(function(){ $.Ajax({ url: $(this).attr("href"), type: 'get', complete: function(response){ if(response.status == 404){ location.href="HTTP://www.baidu.com/404.html"; alert('无效'); }else{ alert('有效'); } } }); }); });
方法二:(ajax xmlHTTP方法: 使用ActiveXObject,所以仅支持ie,非IE内核浏览器不可用。)
<script type="text/Javascript"> function chkurl(url) { var xmlhttp = new ActiveXObject( "microsoft.XMLHTTP"); xmlhttp.open("GET",url,false); xmlhttp.send(); if(xmlhttp.readyState==4){ if(xmlhttp.Status != 200) alert("不存在") else alert("存在") } } </script> <a href="http://www.baidu.com/" onclick="javascript:return chkurl(this.href);">【ajax检测链接有效性】</a>








网友评论文明上网理性发言 已有0人参与
发表评论: