Introduction
Sometime when internet is disconnected, we have
to check it again and again that is internet is connected? So this script does
this task for us.
This script checks the connectivity of internet connection
after every 20 seconds. If the internet is not connected, it just displays
the message that internet is not disconnected. When the system is connected to
internet, it shows the message that "Internet is connected."
Using the code
In this script we are sending a request to web page using JavaScript
object XmlhttpRequest. If the request is unsuccessful, it will try to reconnect
again after 20 seconds. Other wise in case of success it shows a message that "Internet
is connected"
var xmlhttp
function ConnectToNet(url)
{
xmlhttp=null;
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
} catch (e) {
}
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest()
}
else if (window.ActiveXObject){
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
}
if (xmlhttp!=null){
xmlhttp.onreadystatechange=state_Change
xmlhttp.open("GET",url,true)
xmlhttp.send(null)
}
else{
alert("Your browser does not support XMLHTTP.")
}
}
function state_Change()
{
if (xmlhttp.readyState==4){
try{
if (xmlhttp.status==200){
var objDiv = document.getElementById('div1');
objDiv.innerHTML = "<font color=blue>Internet is connected.</font>";
alert("Internet is Connected.");
return;
}
else{
alert("Problem retrieving XML data")
}
} catch(err){
var objDiv = document.getElementById('div1');
objDiv.innerHTML += "<font color=red>Internet is not connected.<br/></font>";
setTimeout("ConnectToNet('http://www.google.com')",20000);
}
}
}