Check if server is online or offline using javascript.

To test the code below:
1. Put the code below in test.html
2. Open the html file using IIS Server.
3. Wait every 10secs to get the server status.

To test if the code was able to get the correct status of the server do the following:
* Turn OFF IIS Server:
- In Start menu -> Run -> type: "iisreset /stop"

* Turn ON IIS Server:
- In Start menu -> Run -> type: "iisreset /start"


filename: test.html

<html>
<head>
<title></title>
<script type="text/javascript">

var ServerObject = {
xmlhttp: null,
checkerId : 0,

checkServerStatus: function()
{
ServerObject.xmlhttp = null; //initialize value as null so that the default value is null and not undefined.
ServerObject.xmlhttp = ServerObject.getXmlHttpRequestObject();
var url = "#"; //Have to use # symbol instead of actual page/document.location.href so that the server could return correct xmlhttp.status code;
try {
//Allows reading of privileged or sensitive data from the browser. This allows the script to pass the same origin check for any document.
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
} catch (e) {
//For IE it comes here.
//alert("Permission UniversalBrowserRead denied.");
}

if (ServerObject.xmlhttp!=null){
//Some versions of some Mozilla browsers won't work properly if the response from the server doesn't have an XML mime-type header.
//xmlhttp.overrideMimeType('text/xml');
ServerObject.xmlhttp.onreadystatechange = ServerObject.stateChange;
ServerObject.xmlhttp.open("GET",url,true);
ServerObject.xmlhttp.send(null);
}
else{
alert("Your browser does not support XMLHTTP.");
}
},

stateChange: function()
{
if (ServerObject.xmlhttp.readyState==4){
//alert(xmlhttp.status); //for testing

//if server is unavailable or IIS was stopped, the xmlhttp.status becomes 0 (in FF) and 12031 or 12029 (in IE).
//Visit http://support.microsoft.com/kb/193625 for a better explaination about the meanng of the numbers returned by xmlhttp.status.
if (ServerObject.xmlhttp.status == 0 || (ServerObject.xmlhttp.status >= 12029 && ServerObject.xmlhttp.status <= 12031))
{
//Server is offline.
document.getElementById('status').innerHTML = "Server is offline";
//ServerObject.stopChecker();
}
else
{
document.getElementById('status').innerHTML = "Server is online";
}
}
},

//Gets the browser specific XmlHttpRequest Object
getXmlHttpRequestObject: function()
{
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else if(window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
} else {
alert('Status: Cound not create XmlHttpRequest Object. Consider upgrading your browser.');
}
},

startChecker: function()
{
ServerObject.checkerId = setInterval ( "ServerObject.checkServerStatus()", 10000 );
},

stopChecker: function()
{
clearInterval(ServerObject.checkerId);
}
}

var counter = 1;
function startTime()
{
counter = (counter >= 10)? 1 : counter + 1;
document.getElementById('txt').innerHTML = counter;

if(counter == 5)
{
document.getElementById('status').innerHTML = null;
}
}

</script>
</head>
<body onload="setInterval ('startTime()',1000); ServerObject.startChecker();">
<form>
<div id="txt"></div> </br>
<div id="status"></div>
</form>
</body>
</html>

No comments:

Post a Comment