Using javascript to determine if a web server is responding.

L

long5120

I am trying to use webpage with javascript to check if a web server is
responding. I was thinking of using 2 frames. Frame1 will have the
site, and the Frame2 will be a status bar (not really needed). This
webpage will call the site's homepage and refresh in 10 seconds. If I
can see the homepage then, the site is up, else site is down. I have
an example of my code below.

My question, is it possible to check the homepage in frame1 (after
refreshed) and see if the page comes up. What I would be looking for
is HTTP Errors or "Cannot find server or DNS Error" pages. I was
thinking of reading the page title and compare it with the original,
but I cannot seem to reference the document title within frame1. Or
parse thru the document text in frame1 and look for keywords like
"Cannot find server...". Also another posssibility could be to check
the HTTP header. I have search thru many newsgroups and websites, but
still haven't got a final solution.

Any help would be greatly appreciated.

Thanks...


******************************************************************
Sample Code
******************************************************************
<html>
<SCRIPT LANGUAGE="JavaScript">
site="http://www.timeanddate.com/counters/newyear.html";
delay=10000;
</SCRIPT>

<frameset rows="*,25" border=0 frameborder=1 framespacing=0
onLoad="window.frames[0].location=site;window.setInterval('window.frames[0].location=site',delay)">
<frame name="TopFrame" scrolling=no>
<frame name="BottomFrame" scrolling=no>
</frameset>
</html>






******************************************************************
Variation of Sample Code
******************************************************************
<html>

<script language=javascript>
site="http://www.timeanddate.com/counters/newyear.html";
delay=10000;
function refresh()
{
window.frames[0].location=site;
//code to check if site is up
}
</script>

<frameset rows="*,25" frameborder=1
onLoad="window.setInterval('refresh()',delay)">
<frame name="TopFrame" scrolling=no>
<frame name="BottomFrame" scrolling=no>
</frameset>
</html>
 
R

Randell D.

******************************************************************

I am a newbie and don't know about your code - but I did give an problem
like this some thought a few months ago and came up with a solution that I
have not tried (but I believe it would work). Basically, in Javascript, you
can I believe load an image in to your cache without displaynig it... If you
have access to the remote server, place a very small image (a few bytes) on
it... Attempt to download it and check its size after ten or twenty
seconds... If the size is zero, then you know the remote site is not
available... If it is greater than zero, then your remote site should be up.

Note... you might face some problems with proxies that might answer you,
thus, as a workaround (which again I have not tried) you could request your
image to be downloaded with an arguement tagged to its filename that is
randomized... something
http://www.remotesite.com/images/clear.gif?110341324123 This I believe
should make sure your image is taken from the original server since it
shouldn't exist on the proxy (or at very least, slim chance of it existing
on the remote server).

If you're good enough to script the above solution, I'd appreciate it if you
could share it here in the newsgroup because I for one would make use of it.

Cheers
Randell D.
 
L

Lasse Reichstein Nielsen

Randell D. said:
I am a newbie and don't know about your code - but I did give an problem
like this some thought a few months ago and came up with a solution that I
have not tried (but I believe it would work). Basically, in Javascript, you
can I believe load an image in to your cache without displaynig it... If you
have access to the remote server, place a very small image (a few bytes) on
it... Attempt to download it and check its size after ten or twenty
seconds... If the size is zero, then you know the remote site is not
available... If it is greater than zero, then your remote site should be up.

Using an image is the most compatible method. You shouldn't check
after ten or twenty seconds, that might not be enough (perhaps the
page is in the cache, so the image fetching is the first internet
activity that a person does ... and his modem has to make the call
before requesting the image.

Luckily, images have the "onload" and "onerror" event handlers.
---
function ifUp(url,onUp,onDown) {
// make random string
var RANDOM_DIGITS = 7; // this is sufficient. Don't do more than ~12.
var pow = Math.pow(10,RANDOM_DIGITS);
var randStr = String(Math.floor(Math.random()*pow)+pow).substr(1);
// create and load image
var img = new Image();
img.onload = onUp;
img.onerror = onDown;
img.src = url+"?"+randStr;
}
---
Example use:
---
ifUp("http://www.infimum.dk/privat/PicA.png",
function(){
// do something
alert("Server is responding");
},
function(){
// do something else
alert("Server is *not* responding");
});
---
Note... you might face some problems with proxies that might answer you,
thus, as a workaround (which again I have not tried) you could request your
image to be downloaded with an arguement tagged to its filename that is
randomized... something
http://www.remotesite.com/images/clear.gif?110341324123

This also prevents it from being taken from the browser cache. If you don't
have access to the server, you might not be able to set the "don't cache"
flag.
If you're good enough to script the above solution, I'd appreciate it if you
could share it here in the newsgroup because I for one would make use of it.

Hope you can use it.
Suggestions for improvement are welcome.

/L
 
J

Jim Ley

// make random string
var RANDOM_DIGITS = 7; // this is sufficient. Don't do more than ~12.
var pow = Math.pow(10,RANDOM_DIGITS);
var randStr = String(Math.floor(Math.random()*pow)+pow).substr(1);

Surely a timestamp would be simple and guarantee non collision unless
the user changed their clock, something that is generally unlikely.

Jim.
 
L

long5120

Using an image sounds like a great method, but it will not work for
me.

Reason: The purpose of this page is to be able to hit any website on
the internet without changing anything on the server. I might not have
access to certain servers.
 
T

Thomas 'PointedEars' Lahn

long5120 said:
I am trying to use webpage with javascript to check if a web server is
responding.

What if client-side JavaScript is disabled or not even supported?
Forget about it.
My question, is it possible to check the homepage in frame1 (after
refreshed) and see if the page comes up.

No, the Same Origin Policy will most certainly forbid that.


PointedEars
 
E

Eric Bohlman

(e-mail address removed) (HikksNotAtHome) wrote in
So, you are telling me that page1.html on my local server has no
access to file1.html on your website server? To read it, check its
existence, do what I want with the code?

Yep. If a script from a page on one server could read out of a window
showing a page from another server, it could do things like grabbing
passwords, or just reporting back to its owner about what sites you're
viewing. Not exactly the picture of privacy.
 
T

Thomas 'PointedEars' Lahn

Eric said:
(e-mail address removed) (HikksNotAtHome) wrote
[Same Origin Policy?]

Yep. If a script from a page on one server could read out of a window
showing a page from another server, it could do things like grabbing
passwords, or just reporting back to its owner about what sites you're
viewing. Not exactly the picture of privacy.

True, but the SOP it is *not* a matter of the *server*, but of the
*domain*, protocol and port of the requested resource. Neither one
is allowed to differ from that of the requesting resource in unsigned
scripts[1], with the exception that resources of deeper domain levels
are allowed to be accessed by resources of the same Second-Level-Domain
if the `document.domain' property was set:

http://www.mozilla.org/projects/security/components/same-origin.html

AFAIK this applies to the IE browser component as well.


PointedEars
___________
[1] http://www.mozilla.org/projects/security/components/jssec.html
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
function ifUp(url,onUp,onDown) {
// make random string
var RANDOM_DIGITS = 7; // this is sufficient. Don't do more than ~12.
var pow = Math.pow(10,RANDOM_DIGITS);
var randStr = String(Math.floor(Math.random()*pow)+pow).substr(1);
// create and load image
var img = new Image();
img.onload = onUp;
img.onerror = onDown;
img.src = url+"?"+randStr;
}


IMHO, randStr could be new Date().


AFAICS, that function implies a means whereby a plain javascript page can
check the time setting of the displaying computer, given a co-operating
server. The page calls for an image URL + "?" + T1 + "," + T2 and an
image is returned if the time (GMT) is within T1..T2 inclusive.

By replacing T1 with 0 or T2 with 8e15 one can also tell whether the
present time is before T2 or after T1.

http://www.thegoonshow.co.uk/scripts/punch-up.html is slightly relevant.
 
L

long5120

Thank you everybody for contributing to my problem. I think I found a
possible solution. Due to security reasons, I cannot read from a page
on a different domain, but I can read from a local page. So, I will
use code that will read from a local file. When IE request for a
webpage on a server, if the server is unable to respond, then a local
"Server not responding" error file is loaded. By using a try catch
block, I'll read from this local "Server not responding" error file. I
hope this makes sense. I have tested the following code and it appears
to work.

Please post any questions/comments to the following code.

thank you,
Long

**************************************************************************
Sample Code
**************************************************************************
<SCRIPT LANGUAGE="JavaScript">

site="http://www.timeanddate.com/counters/newyear.html";

delay=1*60000;

function update()
{
//Reload this page
window.document.location.reload();

//Check if server is responding
try {
if (window.parent.frame1.document.title == "Cannot find server" ||
window.parent.frame1.document.title == "No page to display") {
//Server is not working, perform the following code
alert("Server is not working");
}
}
catch(e){
//Server is working, perform the following code
alert("Server is working");
}
}
</SCRIPT>

<html>
<head>
<title>Webpage Refresh</title>
<meta HTTP-EQUIV="Pragma" CONTENT="Nocache">
</head>
<frameset rows="*,0" border=0 frameborder=1 framespacing=0
onLoad="window.frames[0].location=site;window.setInterval('update()',
delay)">
<frame name="frame1">
</frameset>
</html>
 
R

Randell D.

Randell D. said:
I am a newbie and don't know about your code - but I did give an problem
like this some thought a few months ago and came up with a solution that I
have not tried (but I believe it would work). Basically, in Javascript, you
can I believe load an image in to your cache without displaynig it... If you
have access to the remote server, place a very small image (a few bytes) on
it... Attempt to download it and check its size after ten or twenty
seconds... If the size is zero, then you know the remote site is not
available... If it is greater than zero, then your remote site should be
up.

Using an image is the most compatible method. You shouldn't check
after ten or twenty seconds, that might not be enough (perhaps the
page is in the cache, so the image fetching is the first internet
activity that a person does ... and his modem has to make the call
before requesting the image.

Luckily, images have the "onload" and "onerror" event handlers.
---
function ifUp(url,onUp,onDown) {
// make random string
var RANDOM_DIGITS = 7; // this is sufficient. Don't do more than ~12.
var pow = Math.pow(10,RANDOM_DIGITS);
var randStr = String(Math.floor(Math.random()*pow)+pow).substr(1);
// create and load image
var img = new Image();
img.onload = onUp;
img.onerror = onDown;
img.src = url+"?"+randStr;
}
---
Example use:
---
ifUp("http://www.infimum.dk/privat/PicA.png",
function(){
// do something
alert("Server is responding");
},
function(){
// do something else
alert("Server is *not* responding");
});
---
Note... you might face some problems with proxies that might answer you,
thus, as a workaround (which again I have not tried) you could request your
image to be downloaded with an arguement tagged to its filename that is
randomized... something
http://www.remotesite.com/images/clear.gif?110341324123

This also prevents it from being taken from the browser cache. If you don't
have access to the server, you might not be able to set the "don't cache"
flag.
If you're good enough to script the above solution, I'd appreciate it if you
could share it here in the newsgroup because I for one would make use of
it.

Hope you can use it.
Suggestions for improvement are welcome.

/L
--
Lasse Reichstein Nielsen - (e-mail address removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'

Even though I did not raise the original post, but instead, proposed the
solution (but didn't have the skills to implement) I definetly like what you
just wrote... and have saved it in my notes for later reference...

cheers
randelld
 
R

Randell D.

Using an image sounds like a great method, but it will not work for
me.

Reason: The purpose of this page is to be able to hit any website on
the internet without changing anything on the server. I might not have
access to certain servers.



==

Another suggestion - You've not said that you want to use anything from the
remote server / website - so you could try using the code provided by Lasse
Nielsen in an earlier post to check for one of four different files instead
of an image (CERN (Unix) based web servers default to checking for
index.html or index.htm in a web servers root directory and I think
Microsoft servers default to displaying default.htm or default.html)... I
think you could preload the web page html code (though any javascript or
images or other non-html code would not be preloaded)....

Its worth a try...

Keep in mind though that preloading could be timeconsuming so you'll want to
test more than once or twice...

Hope the suggestion works...

randelld
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top