使用javascript确定Web服务器是否正在响应。 [英] Using javascript to determine if a web server is responding.

查看:76
本文介绍了使用javascript确定Web服务器是否正在响应。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用带有javascript的网页来检查网络服务器是否为
响应。我在考虑使用2帧。 Frame1将拥有

站点,而Frame2将成为状态栏(不是真的需要)。这个

的网页将调用该网站的主页并在10秒内刷新。如果我

然后可以看到主页,那么网站就会启动,否则网站就会关闭。我有

我的代码示例如下。


我的问题是,是否可以检查第1帧中的主页(
$ b $之后) b刷新)并查看页面是否出现。我要找的是

是HTTP错误还是找不到服务器或DNS错误页面。我想读取页面标题并将其与原始版本进行比较,

但我似乎无法在frame1中引用文档标题。或者

通过frame1中的文档文本进行解析,并查找关键字,例如

无法找到服务器...。另一个可能性是检查HTTP标头的
。我通过许多新闻组和网站搜索,但是

仍然没有最终的解决方案。


任何帮助将不胜感激。


谢谢......

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

示例代码

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

< 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'' ,延迟)">

< frame name =" TopFrame" scrolling = no>

< frame name =" BottomFrame" scrolling = no>

< / frameset>

< / html>


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

示例代码的变化

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

< html>


< script language = javascript>

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

delay = 10000;

函数刷新()

{

window .frames [0] .location = site;

//检查网站是否已启动的代码

}

< / script>


< frameset rows =" *,25" frameborder = 1

onLoad =" window.setInterval(''refresh()'',delay)">

< frame name =" TopFrame" scrolling = no>

< frame name =" BottomFrame" scrolling = no>

< / frameset>

< / html>

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>

推荐答案




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


我是新手,不要知道你的代码 - 但我确实提出了一个问题

就像几个月前的一些想法一样,想出了一个解决方案,我没有尝试过b $ b但是我相信它会工作)。基本上,在Javascript中,你我可以相信将图像加载到你的缓存而不显示它...如果你

可以访问远程服务器,那就放一个

上的小图像(几个字节)它...尝试下载它并在十或二十之后检查它的大小

秒...如果大小为零,然后你知道远程站点不是可用的b $ b ...如果它大于零,那么你的远程站点应该是up。


注意......你可能会遇到一些代理可能会回答你的问题,

因此,作为一种解决方法(我还没试过)你可以要求下载

图像标记为其文件名的争论随机化......
http://www.remotesite.com/images/clear.gif?110341324123 我相信

应该确保你的图片来自原始服务器因为它代理上不应该存在
(或者至少在远程服务器上存在很少的机会)
>
如果你足够好以编写上述解决方案的脚本,如果你能够在新闻组中分享它,我会很感激,因为我会用它来使用它。


干杯

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.


" Randell D." <再********************** @ and.share.com>写道:
"Randell D." <re**********************@and.share.com> writes:
我是一个新手,不知道你的代码 - 但我确实提出了一个问题
像几个月前的一些想法并想出了我没有尝试过的解决方案(但我相信它会起作用)。基本上,在Javascript中,您可以相信将图像加载到缓存中而不显示它...如果您有权访问远程服务器,请放置一个非常小的图像(几个字节)
它...尝试下载它并在十或二十秒后检查它的大小......如果大小为零,那么你知道远程站点不可用......如果它大于零,那么您的远程站点应该已启动。


使用图像是最兼容的方法。十或二十秒后你不应该检查

,这可能还不够(也许

页面在缓存中,所以图像提取是第一个互联网
一个人做的
活动......他的调制解调器必须在请求图像之前拨打电话




幸运,图像具有onload和onerror事件处理程序。

---

函数ifUp(url,onUp,onDown){

//制作随机字符串

var RANDOM_DIGITS = 7; //这就足够了。不要超过~12。

var pow =数学。 pow(10,RANDOM_DIGITS);

var randStr = String(Math.floor(Math.random()* pow)+ pow).substr(1);

/ /创建并加载图片

var img = new Image();

img.onload = onUp;

img.onerror = onDown;

img.src = url +"?" + randStr;

}

---

示例使用:

---

ifUp(" http://www.infimum.dk/p rivat / PicA.png",

function(){

//做点什么

alert(服务器正在响应); < br $>
},

function(){

//做其他事情

alert(" Server is * not *回复);

});

---

注意......你可能会遇到一些可能会回答你问题的代理问题,因此,作为一种解决方法(我还没有尝试过),您可以请求下载带有标记为其文件名的争论的
图像随机化......某事
http://www.remotesite.com/images/clear。 gif?110341324123

这也可以防止它被从浏览器缓存中删除。如果你没有
可以访问服务器,你可能无法设置不要缓存

标志。

如果你足够好以编写上述解决方案的脚本,如果你能在新闻组中分享它,我将不胜感激,因为我可以使用它。
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.




希望你能用它。

欢迎提出改进建议。


/ L

-

Lasse Reichstein Nielsen - lr*@hotpop.com

DHTML死亡色彩:< URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>

''没有判断力的信仰只会降低精神神圣。''



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.''


2003年12月27日星期六12:51:47 +0100,Lasse Reichstein Nielsen

< lr*@hotpop.com>写道:
On Sat, 27 Dec 2003 12:51:47 +0100, Lasse Reichstein Nielsen
<lr*@hotpop.com> wrote:
//随机字符串
var RANDOM_DIGITS = 7; //这就足够了不要超过~12。
var pow = Math.pow(10,RANDOM_DIGITS);
var randStr = String(Math.floor(Math.random()* pow)+ pow) .substr(1);
// 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 );




当然时间戳很简单并保证不会发生冲突,除非用户改变了他们的时钟,这是一般不太可能。


吉姆。

-

comp.lang.javascript常见问题 - http://jibbering.com/faq/



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/


这篇关于使用javascript确定Web服务器是否正在响应。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆