Detecting website status with Javascript

December 1, 2012

The following method allows you to check whether a third party site is responding to http requests or not. The method bases its result by trying to download somesite.com/favicon.ico file , but in reality it could be any other image file.

function isSiteOnline(url,callback) {
    // try to load favicon
    var timer = setTimeout(function(){
        // timeout after 5 seconds
        callback(false);
    },5000)

    var img = document.createElement("img");
    img.onload = function() {
        clearTimeout(timer);
        callback(true);
    }

    img.onerror = function() {
        clearTimeout(timer);
        callback(false);
    }

    img.src = url+"/favicon.ico";
}

Usage

isSiteOnline("http://www.facebook.com",function(found){
    if(found) {
        // site is online
    }
    else {
        // site is offline (or favicon not found, or server is too slow)
    }
})

Working Example

http://

Original Stack Overflow question and my answer:

http://stackoverflow.com/a/13664860/149636