KISS

This post has nothing to do with any musician like kiss or ac/dc which should be popular during this crisis. It’s just all about keeping it simple.

I needed some code to make it possible to check if my computer at home was online. It’s all related to the stuff you can find in my java3 documentation. My original plan was using a little bit of AJAX which could check if my server was reachable and update a part of the page if so.

After some googling and putting stuff together I got a javascript like this


var xmlHttp;

function showOnline()
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
url='http://home.tisnix.be/online.txt?';
url=url+"sid="+Math.random();

xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open('GET',url , true);
xmlHttp.send(null);

}
function stateChanged()
{
if (xmlHttp.readyState==4) {
document.getElementById("txtResult").textContent=xmlHttp.responseText;
}
}

The code for GetXmlHttpObject has been copied from w3schools and looks like this


function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}

The code has been working but it got a little bug. I first tried it on my computer where it was working as it should. But porting it to the scarlet pages, my javascript debugger showed me some errors. With GetXmlHttpObject you can’t open pages on another domain. So I got stuck.

But there was a solution. The solution wasn’t vodka but putting the javascript on my local computer. So just adding this little script to my computer did the trick:

function showOnline(str) {
document.getElementById("txtOnline").textContent=str;
}

So the javascript page gets loaded from my computer. If it’s not online, you don’t get any annoying error unless you’re doing some debugging. If it gets loaded, the function replaces the element with id txtOnline with the argument you gave it. So I really had to keep it simple.

UPDATE: my test doesn’t work. I really have to do it asynchronously or the page hangs

UPDATE2: a working solution can be found in kiss part 2


One Comment on “KISS”

  1. […] (part 2) I previously tried to create an easy solution to detect if my computer was up and running but I got stuck with […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s