Click here to Skip to main content
Licence CPOL
First Posted 21 Aug 2007
Views 23,473
Downloads 233
Bookmarked 16 times

Test Internet Connection using AJAX

By | 21 Aug 2007 | Article
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".

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

Member



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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberRanjan.D2:27 22 Jan '12  
QuestionAllways appears me Problem retrieving XML data PinmemberHiber Tadeo8:02 16 Dec '11  
GeneralSlight modification Pinmemberrippo9:46 21 Aug '07  
GeneralRe: Slight modification PinmemberAdeel Hussain19:12 21 Aug '07  

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

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

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