Click here to Skip to main content
12,761,826 members (41,690 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 20 Aug 2007

Test Internet Connection using AJAX

, 21 Aug 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
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.


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)
    try {"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){
        alert("Your browser does not support XMLHTTP.")

function state_Change()
    // if xmlhttp shows "loaded"
    if (xmlhttp.readyState==4){
              // if "OK"    
            if (xmlhttp.status==200){
            var objDiv = document.getElementById('div1');
            objDiv.innerHTML = "<font color=blue>Internet is connected.</font>";
            alert("Internet is Connected.");
            alert("Problem retrieving XML data")
    } catch(err){
        var objDiv = document.getElementById('div1');
        objDiv.innerHTML += "<font color=red>Internet is not connected.<br/></font>";


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

You may also be interested in...


Comments and Discussions

GeneralMy vote of 1 Pin
Ranjan.D22-Jan-12 3:27
memberRanjan.D22-Jan-12 3:27 
QuestionAllways appears me Problem retrieving XML data Pin
Hiber Tadeo16-Dec-11 9:02
memberHiber Tadeo16-Dec-11 9:02 
GeneralSlight modification Pin
rippo21-Aug-07 10:46
memberrippo21-Aug-07 10:46 
GeneralRe: Slight modification Pin
Adeel Hussain21-Aug-07 20:12
memberAdeel Hussain21-Aug-07 20: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.

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