Jquery checks whether the page url is valid. jquery checks the page
Method 1 :(JQuery method:Applicable to all browsers)
HTML page:
<! DOCTYPE html>
<Html>
<Head lang = "en">
<Meta charset = "UTF-8">
<Title> </title>
<Script type = "text/javascript" src = "js/jquery-1.7.1.min.js"> </script>
</Head>
<Body>
<A href = "http://www.baidu.com/"> [jquery detection link validity] </a>
<A href = "www.baidu.com/"> [jquery checks link validity 2] </a>
// <Script type = "text/javascript" src = "js/base. js"> </script>
</Body>
</Html>
JS page:
// Determine the address Validity
$ ("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 ('invalid ');
} Else {
Alert ('payby ');
}
}
});
});
});
Method 2: (ajax xmlhttp method: ActiveXObject is used. Therefore, only IE is supported and non-IE kernel browsers are unavailable .)
<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(
"Does not exist"
)
else
alert(
"Exist"
)
}
}
</script>
<a href=
"http://www.baidu.com/"
onclick=
"javascript:return chkurl(this.href);"
> [Ajax check link validity] </a>
Extended learning:
404. The Web server cannot find the file or script you requested. Check the URL to ensure that the path is correct.