1

I'm writing a small app that will be used to monitor the status of a few websites, mainly just to report which websites are online and which websites are offline.

I currently have this code as the onreadystatechange function:

if(xmlhttp.readyState == 4) {
    if (xmlhttp.status == 200) {
        element(id).innerHTML = "Online";
    }
    else {
        element(id).innerHTML = "Offline";
    }
}

It runs properly when the website is online, but it never reaches the 'else' block if I do an AJAX request on a website that is offline.. I'm thinking the request never reaches readyState 4 if the website is down?

Any suggestions for how to capture an AJAX request to an offline website?

ampersandre
  • 3,156
  • 3
  • 29
  • 37
  • An alternative approach is to have a hidden IFRAME pointed at each page, and put onload and onerror event handlers on each. – EricLaw Aug 07 '09 at 15:31

4 Answers4

1

What you will want to do is abort the request after a specific amount of time, i.e. a timeout. Here's an article that should help:

Async Requests over an Unreliable Network

karim79
  • 339,989
  • 67
  • 413
  • 406
  • Yeah, I did some investigating, and when you do an AJAX request to a resource that is offline, the readyState never reaches 4, it stays at 1 until you abort the request. – ampersandre Aug 06 '09 at 22:52
1

IE8's XHR object supports an ontimeout event (http://msdn.microsoft.com/en-us/library/cc197061(VS.85).aspx)

ReadyState=4 means "loaded", which won't happen if the content doesn't load.

EricLaw
  • 56,563
  • 7
  • 151
  • 196
0

I just tested now using Fiddler, and it returned Status 502, Body 512

Amr Elgarhy
  • 66,568
  • 69
  • 184
  • 301
-1

You can check the header for a 404 error.

RiddlerDev
  • 7,370
  • 5
  • 46
  • 62