By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,567 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,567 IT Pros & Developers. It's quick & easy.

Using javascript to determine if a web server is responding.

P: n/a
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>
Jul 20 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a


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

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.
Jul 20 '05 #2

P: n/a
"Randell D." <re**********************@and.share.com> writes:
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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3

P: n/a
On Sat, 27 Dec 2003 12:51:47 +0100, Lasse Reichstein Nielsen
<lr*@hotpop.com> wrote:
// 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.
--
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #4

P: n/a
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.
Jul 20 '05 #5

P: n/a
long5120 wrote:
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
Jul 20 '05 #6

P: n/a
In article <3F**************@PointedEars.de>, Thomas 'PointedEars' Lahn
<Po*********@web.de> writes:
No, the Same Origin Policy will most certainly forbid that.


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?

The OP didn't ask to manipulate the page, just ensure that the file existed.
That I *can* do. I just haven't grasped all the readyState codes yet to
understand what each one means.

Use an HTTPResponse Object. If it retrieves the file, then the servers up. If
it doesn't then the server is either down or the file doesn't exist.
--
Randy
Jul 20 '05 #7

P: n/a
hi************@aol.com (HikksNotAtHome) wrote in
news:20***************************@mb-m06.aol.com:
In article <3F**************@PointedEars.de>, Thomas 'PointedEars'
Lahn <Po*********@web.de> writes:
No, the Same Origin Policy will most certainly forbid that.


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.
Jul 20 '05 #8

P: n/a
Eric Bohlman wrote:
hi************@aol.com (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/secu...me-origin.html

AFAIK this applies to the IE browser component as well.
PointedEars
___________
[1] http://www.mozilla.org/projects/secu...nts/jssec.html
Jul 20 '05 #9

P: n/a
JRS: In article <d6**********@hotpop.com>, seen in
news:comp.lang.javascript, Lasse Reichstein Nielsen <lr*@hotpop.com>
posted at Sat, 27 Dec 2003 12:51:47 :-
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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jul 20 '05 #10

P: n/a
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>
Jul 20 '05 #11

P: n/a
In article <Xn*******************************@130.133.1.4>, Eric Bohlman
<eb******@earthlink.net> writes:

hi************@aol.com (HikksNotAtHome) wrote in
news:20***************************@mb-m06.aol.com:
In article <3F**************@PointedEars.de>, Thomas 'PointedEars'
Lahn <Po*********@web.de> writes:
No, the Same Origin Policy will most certainly forbid that.


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.


I have a page running locally that uses an HTTPRequest in IE to load files from
all over the web. It shows me the source code in a text box.

All the OP wanted to know was "Is the server up?", and I believe an HTTPRequest
can tell me that. Not load the page and try to access it (the SOP comes in),
but with an HTTPRequest, it doesn't.

http://www.jibbering.com/faq/#FAQ4_34

Will lead you to:

http://jibbering.com/2002/4/httprequest.html

Where it discusses executing server side scripts (mostly), but it can also be
used to simply attempt to load a file from a server.

What I have not had time to research yet is what the different readyState's
tell me.
--
Randy
Jul 20 '05 #12

P: n/a

"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:d6**********@hotpop.com...
"Randell D." <re**********************@and.share.com> writes:
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 - lr*@hotpop.com
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

Jul 20 '05 #13

P: n/a

"long5120" <gh*******@carolina.rr.com> wrote in message
news:83**************************@posting.google.c om...
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
Jul 20 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.