Click here to Skip to main content
15,880,608 members
Articles / Programming Languages / Javascript

Test Internet Connection using AJAX

Rate me:
Please Sign up or sign in to vote.
1.91/5 (11 votes)
21 Aug 2007CPOL 56.1K   863   16   4
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.

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".

JavaScript
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)


Written By
Web Developer Allainet (Pvt) Ltd
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Ranjan.D22-Jan-12 2:27
professionalRanjan.D22-Jan-12 2:27 
QuestionAllways appears me Problem retrieving XML data Pin
Hiber Tadeo16-Dec-11 8:02
Hiber Tadeo16-Dec-11 8:02 
GeneralSlight modification Pin
rippo21-Aug-07 9:46
rippo21-Aug-07 9:46 
GeneralRe: Slight modification Pin
Adeel Hussain21-Aug-07 19:12
Adeel Hussain21-Aug-07 19:12 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.