Jquery checks whether the page url is valid. jquery checks the page

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.