...

View Full Version : Javascript not working as desired



layman
12-15-2009, 12:25 PM
Greetings to Everyone
Professionally I'm not a programmer. So i have no tough idea about variables,loops,functions etcetra, So can anybody help assemble this piece of Javascript to work properly.
In both cases the script show either "OK" or "Down"
i can't make it to work.
I hope a programming guru will help me to fix this javascript code.


<html>
<head>
<title>My Intranet Page</title>
</head>
<body>
<script>
function doNoConnectFunction() {
document.getElementById("intstatus").innerHTML = "Internet is Down";
document.getElementById("wsstatus").innerHTML="Internal Web Server is Down";
document.getElementById("fntcolor").style.color="#0000FF";
}

function doConnectFunction() {
document.getElementById("intstatus").innerHTML = "Internet is Ok";
document.getElementById("wsstatus").innerHTML="Internal Web Server is Ok";
document.getElementById("fntcolor").style.color="#FF0000";
}

var i = new Image();
i.onload = doConnectFunction;
i.onerror = doNoConnectFunction;
i.src = 'http://www.aapress.com.au/img/71f.gif?d=' + escape(Date());

var j = new Image();
j.onload = doConnectFunction;
j.onerror = doNoConnectFunction;
j.src = 'http://192.168.0.2/imgages/err.jpg' + escape(Date());

</script>

<font face="Arial">

<b><font size="4">Works only on Javascript Enabled Browsers</font></b>
<br>
<br>
<table border=1 cellpadding=2>
<tr>
<td align=right valign=top>Internet Status</td>
<td><span id="fntcolor"><span id="intstatus"></span><font></td>
</tr>
<tr>
<td align=right valign=top>Web Server Status</td>
<td><span id="fntcolor"><span id="wsstatus"></span><font></td>
</tr>
</table>
</body>
</html>

Thankyou for taking time to analyze the script
layman

Old Pedant
12-15-2009, 10:01 PM
I see one error:


j.src = 'http://192.168.0.2/imgages/err.jpg' + escape(Date());

That will produce an image named, for example, "err.jpgTuesday December 15..."

You need a question mark there.

I also suspect that "imgages" is spelled wrong.



j.src = 'http://192.168.0.2/images/err.jpg?' + escape(Date());

I assume the purpose of this is to do "cache busting" so that the browser won't use the image it has stored in its cache??

In that case, you'd be better off doing:


j.src = 'http://192.168.0.2/images/err.jpg?' + (new Date()).getTime();


But you know, as that code is written, an error from *either* image will say that both the internet and intranet are down.

Oh, wait...is that what you were asking about??? The problem you need solved??

Old Pedant
12-15-2009, 10:10 PM
Try this:

<html>
<head>
<title>My Intranet Page</title>
<script type="script/javascript">
function showConnection(which,okay)
{
var cell = document.getElementById(which + "status");
cell.innerHTML = "Internet is " + ( okay ? "Ok" : "Down");
cell.style.color = ( okay ? "#00FF00" : "#FF0000" );
}

var i = new Image();
i.onload = function( ) { showConnection("int",true); }
i.onerror = function( ) { showConnection("int",false); }
i.src = 'http://www.aapress.com.au/img/71f.gif?d=' + (new Date()).getTime();

var j = new Image();
j.onload = function( ) { showConnection("ws",true); }
j.onerror = function( ) { showConnection("ws",false); }
j.src = 'http://192.168.0.2/images/err.jpg?d=' + (new Date()).getTime();
</script>
<style type="text/css">
* { font-family: Arial, sans-serif; }
td { font-size: medium; }
span { font-size: large; font-weight: bold; }
</style>
</head>
<body>
<span>Works only on Javascript Enabled Browsers</span>
<br>
<br>
<table border="1" cellpadding="2">
<tr>
<td style="text-align: right;">Internet Status</td>
<td id="intstatus">waiting...</td>
</tr>
<tr>
<td style="text-align: right;">Web Server Status</td>
<td id="wsstatus">waiting...</td>
</tr>
</table>
</body>
</html>

layman
12-16-2009, 12:52 PM
hi Oldpedant
the script didn't worked i thought that replacing "intstatus" and "wsstatus" with "int" and "ws" would work but didn't work either it just displays "waiting...."


<html>
<head>
<title>My Intranet Page</title>
<script type="script/javascript">
function showConnection(which,okay)
{
var cell = document.getElementById(which + "status");
cell.innerHTML = "Internet is " + ( okay ? "Ok" : "Down");
cell.style.color = ( okay ? "#00FF00" : "#FF0000" );
}

var i = new Image();
i.onload = function( ) { showConnection("int",true); }
i.onerror = function( ) { showConnection("int",false); }
i.src = 'http://www.aapress.com.au/img/71f.gif?d=' + (new Date()).getTime();

var j = new Image();
j.onload = function( ) { showConnection("ws",true); }
j.onerror = function( ) { showConnection("ws",false); }
j.src = 'http://192.168.0.2/images/err.jpg?d=' + (new Date()).getTime();
</script>
<style type="text/css">
* { font-family: Arial, sans-serif; }
td { font-size: medium; }
span { font-size: large; font-weight: bold; }
</style>
</head>
<body>
<span>Works only on Javascript Enabled Browsers</span>
<br>
<br>
<table border="1" cellpadding="2">
<tr>
<td style="text-align: right;">Internet Status</td>
<td id="int">waiting...</td>
</tr>
<tr>
<td style="text-align: right;">Web Server Status</td>
<td id="ws">waiting...</td>
</tr>
</table>
</body>
</html>

i think the script is very near to completion but cant' find out where it's going wrong.
thanks

Old Pedant
12-16-2009, 08:46 PM
Tested and working:


<html>
<head>
<title>My Intranet Page</title>
<script type="text/javascript">
function initTests()
{
var i = document.getElementById("testimage1");
i.src = "http://www.artsofsnohomish.org/images/Santa2009Top.jpg?foo=" + (new Date()).getTime();
var j = document.getElementById("testimage2");
j.src = "http://localhost/nosuchthing.jpg?foo=" + (new Date()).getTime();
}
function showConnection(which,okay)
{
var cell = document.getElementById(which + "status");
cell.innerHTML = "Connection is " + ( okay ? "Ok" : "Down");
cell.style.color = ( okay ? "#00FF00" : "#FF0000" );
}
</script>
<style type="text/css">
* { font-family: Arial, sans-serif; }
td { font-size: medium; }
span { font-size: large; font-weight: bold; }
</style>
</head>
<body onload="initTests()">
<span>Works only on Javascript Enabled Browsers</span>
<br>
<br>
<table border="1" cellpadding="2">
<tr>
<td style="text-align: right;">Internet Status</td>
<td id="intstatus">waiting...</td>
</tr>
<tr>
<td style="text-align: right;">Web Server Status</td>
<td id="wsstatus">waiting...</td>
</tr>
</table>
<div style="visibility: hidden;">
<img id="testimage1" alt="" style="width: 10px; height: 10px;"
onload="showConnection('int',true);" onerror="showConnection('int',false);" />
<img id="testimage2" alt="" style="width: 10px; height: 10px;"
onload="showConnection('ws',true);" onerror="showConnection('ws',false);" />
</div>
</body>
</html>

MSIE, at least, would *not* trigger onload/onerror from an image that existed only as a JS object. So I made the "real" to the browser and they worked.

That image from that Australian site no longer exists, by the by, so it would always give an error. You must use an image that truly exists, out on the internet. I'd suggest some google image or similar.

layman
12-17-2009, 03:17 AM
Dear Old Pedant
Thankyou verymuch for your effort to make this piece of script work properly.
It worked like a charm on IE8 and Firefox 3.5
It took me 4 days to come near around that code after massive "google".
and you did it in ......... by the way, how long did it took you to make this script work. 1/2 hour ? 15 minutes?


anyways... you are "Senior,Fluent & Hard Coder"
always keep up your helping hands.



Thanks Once again && Merry Christmas

layman

Old Pedant
12-17-2009, 04:22 AM
Well, once I gave up and decided to actually test it before posting (I didn't test at all first time), about 10 minutes. Maybe 15. About as fast as I could type, basically. <grin/>

First thing I did was a dirt simple test similar to this:

<script>
var i = new Image();
i.onload = new function() { alert('okay'); }
i.src = "...known good image ...";
</script>

When that didn't work the way I wanted to, I dumped it and went to the actual <img> tags instead.

layman
12-17-2009, 05:25 PM
15Minutes is Excellent.
If there's Programming Race, i suggest you to participate :)

by the way does such game exist ? give all Pariticipants a problem and the winner will be the one who codes the fastest. ;)
The text below is not important.
well during playing with the script i thougt that
google is more reliable than www.aapress.com.au so i replaced this


"http://www.aapress.com.au/img/71f.gif?d="

with this


http://www.google.com/images/yellow_warning.gif

and the script said internet is down but http://www.google.com/images/yellow_warning.gif does exist
so imho is it due to google's security thing
:confused:

Philip M
12-17-2009, 05:31 PM
The text below is not important.
well during playing with the script i thougt that
google is more reliable than www.aapress.com.au so i replaced this

with this


http://www.google.com/images/yellow_warning.gif

and the script said internet is down but http://www.google.com/images/yellow_warning.gif does exist
so imho is it due to google's security thing
:confused:

http://www.google.com/images/yellow_warning.gif must be in quotes.

Old Pedant
12-17-2009, 09:18 PM
Layman is correct.

Try this URL in your browser:
http://www.google.com/images/yellow_warning.gif?foo=1

http://www.google.com/images/yellow_warning.gif?foo=1
Google gives you a 404 error (file not found).

Whereas if you omit the "?foo=1" it works fine.

So Google's servers do *NOT* ignore the extra part of the URL. I'm sure that Google's servers are highly customized for Google's uses, so this is not surprising.

Okay, so look around and find another stable image source that works.

You *CAN* just test this in your browser, without needing that JS code. Just append "?foo=1" to the end of the image URL and see if it still works.

Here is one that works:

http://www.codingforums.com/img/logo.gif?foo=123

layman
12-18-2009, 02:45 AM
http://www.google.com/images/yellow_warning.gif must be in quotes.
Thanks Philip as mentioned by you. It was already inside quotes. Sorry i forgot to mention quotes in my reply.
Thanks

layman
12-18-2009, 02:52 AM
Layman is correct.

Try this URL in your browser:
http://www.google.com/images/yellow_warning.gif?foo=1

http://www.google.com/images/yellow_warning.gif?foo=1
Google gives you a 404 error (file not found).

Whereas if you omit the "?foo=1" it works fine.

So Google's servers do *NOT* ignore the extra part of the URL. I'm sure that Google's servers are highly customized for Google's uses, so this is not surprising.

Okay, so look around and find another stable image source that works.

You *CAN* just test this in your browser, without needing that JS code. Just append "?foo=1" to the end of the image URL and see if it still works.

Here is one that works:

http://www.codingforums.com/img/logo.gif?foo=123


Thanks Old Pedant
Now i got it.
It's google :mad:... Not the Script.
:)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum