5,702,921 members and growing! (22,182 online)
Email Password   helpLost your password?
Web Development » Client side scripting » General     Intermediate

Test Internet Connection using AJAX

By Adeel Hussain

Sometime when internet is disconnected, we have to check it again and again that is internet is connected? So this script does this task for us. This script consists of just one html file.
Javascript, Windows, Visual Studio, Dev

Posted: 21 Aug 2007
Updated: 21 Aug 2007
Views: 7,624
Bookmarked: 9 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
8 votes for this Article.
Popularity: 2.31 Rating: 2.56 out of 5
2 votes, 25.0%
1
1 vote, 12.5%
2
1 vote, 12.5%
3
2 votes, 25.0%
4
2 votes, 25.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

Sometime when internet is disconnected, we have to check it again and again that is internet is connected? So this script does this task for us.

This script checks the connectivity of internet connection after every 20 seconds. If the internet is not connected, it just displays the message that internet is not disconnected. When the system is connected to internet, it shows the message that "Internet is connected."

Using the code

In this script we are sending a request to web page using JavaScript object XmlhttpRequest. If the request is unsuccessful, it will try to reconnect again after 20 seconds. Other wise in case of success it shows a message that "Internet is connected"

 
var xmlhttp
function ConnectToNet(url)
{
    xmlhttp=null;
    try {
        netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
    } catch (e) {

    //For IE it comes here.

    //alert("Permission UniversalBrowserRead denied.");

   }
    // code for Mozilla, etc.

    if (window.XMLHttpRequest){
        xmlhttp=new XMLHttpRequest()
    }
    // code for IE

    else if (window.ActiveXObject){
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
    }
    if (xmlhttp!=null){
        xmlhttp.onreadystatechange=state_Change
        xmlhttp.open("GET",url,true)
        xmlhttp.send(null)
    }
    else{
        alert("Your browser does not support XMLHTTP.")
    }
}

function state_Change()
{
    // if xmlhttp shows "loaded"

    if (xmlhttp.readyState==4){
        try{
              // if "OK"    

            if (xmlhttp.status==200){    
            var objDiv = document.getElementById('div1');
            objDiv.innerHTML = "<font color=blue>Internet is connected.</font>";
            alert("Internet is Connected.");
            return;
        }
          else{
            alert("Problem retrieving XML data")
        }
    } catch(err){
        var objDiv = document.getElementById('div1');    
        objDiv.innerHTML += "<font color=red>Internet is not connected.<br/></font>";
        setTimeout("ConnectToNet('http://www.google.com')",20000);
    }
  }
}
 



License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Adeel Hussain



Occupation: Web Developer
Company: Allainet (Pvt) Ltd
Location: Pakistan Pakistan

Other popular Client side scripting articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralSlight modificationmemberrippo10:46 21 Aug '07  
GeneralRe: Slight modificationmemberAdeel Hussain20:12 21 Aug '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 21 Aug 2007
Editor:
Copyright 2007 by Adeel Hussain
Everything else Copyright © CodeProject, 1999-2008
Web20 | Advertise on the Code Project