Click here to Skip to main content
Click here to Skip to main content

Test Internet Connection using AJAX

By , 21 Aug 2007
 

Introduction

Sometimes when the internet connections is unstable, we have to check again and again if the connection is active? This script does this task for us.

This script checks the connectivity of internet connection every 20 seconds. If internet is not connected, it displays a message that internet is not connected. And, when the system is connected to internet, it shows the message that "Internet is connected."

Using the code

In this script, we send a request to a web page using the JavaScript object XmlhttpRequest. If the request is unsuccessful, it will try to reconnect again after 20 seconds. In the 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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Adeel Hussain
Web Developer Allainet (Pvt) Ltd
Pakistan Pakistan
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 1memberRanjan.D22-Jan-12 2:27 
QuestionAllways appears me Problem retrieving XML datamemberHiber Tadeo16-Dec-11 8:02 
GeneralSlight modificationmemberrippo21-Aug-07 9:46 
GeneralRe: Slight modificationmemberAdeel Hussain21-Aug-07 19:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130617.1 | Last Updated 21 Aug 2007
Article Copyright 2007 by Adeel Hussain
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid