473,386 Members | 1,741 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Using javascript to determine if a web server is responding.

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
13 14561


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

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
"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
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
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
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
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
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
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
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
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
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

"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Steve | last post by:
Hi, I have a private website for 20 people that is similar to a web email client like hotmail. There are two frames, one on the left with links for "New", "History", "Todays" and a frame on the...
3
by: Daniel Borden | last post by:
Big hello to my fellow programmers - I'm new to javascript and PHP and I am wondering if there is a method or function to capture a 404 error in the event a particular html file cannot be found. I...
37
by: Haines Brown | last post by:
I understand that <br /> is marginal in CSS, and so am looking for a substitute for the EOL character. I've failed in both approaches and seek advice. The first thing I tried was to use the...
1
by: Mike | last post by:
When trying to compile (using Visual Web Developer 2005 Express Beta; frameworkv2.0.50215 ) the source code below I get errors (listed below due to the use of ICallBackEventHandler. Ultimately I...
2
by: Cortes | last post by:
Hi all, I've written a *utility* Javascript that I want to make available to many of my friends. All you have to do is to insert some code i wrote in the header. However, this piece of...
1
by: RobG | last post by:
A friend of mine was trying to use a Java application over the web but couldn't get it working. The following reply was sent: "Windows Sharepoint Services (WSS), which hosts the Web Parts that...
1
by: onewebclick | last post by:
Is there a way to detect a browser cache is full using javascript or HTML thorugh a web page and inform the user to clear the cache to improve performance of the website. It looks like google's...
1
by: Lance | last post by:
Hi, Is there a way to tell if a browser is currently capable of javascript? ie if the user has disabled it or not? I understand that the Request.Browser.JavaScript property will tell if a...
10
by: IchBin | last post by:
I am trying to set the state of a radio button. I do not see what I am doing wrong. Sorry, I am new at this.. I need another set of eyes to look at this snip of code. I am trying to set the radio...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.