|
||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionSometime 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 codeIn 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) {
//For IE it comes here.
//alert("Permission UniversalBrowserRead denied.");
}
// code for Mozilla, etc.
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest()
}
// code for IE
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 shows "loaded"
if (xmlhttp.readyState==4){
try{
// if "OK"
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);
}
}
}
|
|||||||||||||||||||||||||||||||||||||||||||||||