It's pretty easy:
When the widget is created, an XmlHttpRequest object is created.
function init()
{
http = new ActiveXObject('Microsoft.XMLHTTP');
checkSite();
}
The checkSite() function checks the site:
If we are not checking yet, we change the checking indicator to the appropriate image and open a HTTP get operation with the url. Then we 'send' nothing, which means get the contents of the site.
When the status of the requests changes, the
onReqDone() function will be called, asynchroniously. That means, this function will not wait for the request to end.
function checkSite()
{
var url = this.parameters("URL").value;
var l = this.layers("status");
if(!isChecking)
{
isChecking = true;
if(l != null)
l.src = "images/checking.png";
http.onreadystatechange = onReqDone;
http.open("GET",url,true);
http.send("");
}
}
When the status of the request changes, this function will be executed. readyState holds the status of the request (hardwired to some values). 4 means, totally done
Next, we get the responseText (the url contents) and hash it to compare it the hash of the previous call. If they differ, we assume the site has changed and update the image accordingly.
function onReqDone()
{
if(4 == http.readyState)
{
//var ok = (200 == http.status);
var oldHash = this.parameters("HASH").value;
var newHash = hash(http.responseText);
var changes = newHash != oldHash;
this.parameters("HASH").value = newHash;
var l = this.layers("status");
if(l != null)
{
if(!changes)
l.src = "images/nochanges.png";
else
l.src="images/changes.png";
}
http = new ActiveXObject('Microsoft.XMLHTTP');
isChecking = false;
}
}
A timer then calls the checkSite() function on a regular basis.
What's Web 2.0????? :confused: