How to check whether a website is Online or Offline using JAVASCRIPT?

How to check whether a website is Online or Offline using JAVASCRIPT?

1. Copy the below code in a notepad and save using.html extension.

<p><strong>Enter the website url below and click Check button to get the status of the server</strong></p>

<p>http:// <input type=’text’ id=’sitehost’/> <input type=’button’ id=’checkhost’ value=’Check’/></p>

<script type=’text/javascript’>
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”;
}

document.getElementById(‘checkhost’).onclick = function() {
    isSiteOnline(“http://”+document.getElementById(‘sitehost’).value,function(result){
        var msg = result ? “Site is online” : “Site is offline”;
        alert(msg);
    })
}

</script>2. Enter any website URL and click Check button to check the status.

Leave a Reply