Js checks to determine whether the Link (URL) is valid (the service is available)

Source: Internet
Author: User

How can I use javascript to determine whether the requested url/link is valid (connectable and available )?

Introduction

There is an address book system deployed on several servers at the same time, but there is a Address Book link on the home page to link to this system. the problem is that sometimes the link points to a server with a failure, so you want to point to other servers when the server is faulty (the service is unavailable.

Solution 1: XMLHTTP

<Script language = "javascript">
Function getURL (url ){
Var xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");
Xmlhttp. open ("GET", url, false );
Xmlhttp. send ();
If (xmlhttp. readyState = 4 ){
If (xmlhttp. Status! = 200) alert ("nonexistent ");
Return xmlhttp. Status = 200;
}
Return false;
}
</Script>
<A href = "http://www.bkjia.com/aaa.asp" onclick = "return getURL (this. href)"> csdn </a>

Disadvantage: ActiveXObject is used, so it is IE Only. Non-IE kernel browsers are not available.

 

Solution 2: jQuery Extension

For the following content, refer to [1].

Home: http://plugins.jquery.com/project/linkchecker
Demo page: http://sidashin.ru/linkchecker/
A call example is provided in the downloaded package.
Supplement:
If jQuery is used for a specific URL, you do not need a plug-in as follows:
$. Ajax ({
Url: 'http: // some.url.com ',
Type: 'get ',
Complete: function (response ){
If (response. status = 200 ){
Alert ('payby ');
} Else {
Alert ('invalid ');
}
}
});

Related Article

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.