Is there a JavaScript solution for this problem?

First off, I hardly know Jack Sprat when it comes to JavaScript and I only have a working knowledge of HTML, but I think my solution will call for JavaScript, so I am turning to those with more expertise.

What I am looking for is a way to embed a “ping” into a link; that is, I need something that will inform a user ahead of time (before clicking on the link) to know if the server is up or not and to display the status of the server. I am thinking that there must be some relatively simple Javascript routine out there that accomplishes this seemingly simple task.

I have Googled the hell out of this matter and have come up with nothing so far. If you know of a quick and dirty way to do this either post a snippet of code I can copy and paste, or point me to a link where I can get the code I need.

Javascript does not have access to the TCP/IP stack and therefore can’t send a ping.

I don’t think there’s any way to catch an HTTP timeout either, so that won’t work.

Instead, perhaps there’s a server-side solution. Have a simple cronjob run a program that pings the remote server, and updates the text on the web page accordingly.

Hmm… I’ve been thinking about this and I’m not sure either. You could try looking at ajax and seeing if you can use that to grab something from the server and test it, but I can’t think of an easy snippet offhand.

The other thought that occurs to me wouldn’t require javascript, just embeding an image from the remote server into the link… however, for this to work reliably, you’d need to find a way to keep the image from caching on a local browser when the site is up and thus continuing to display ‘okay’ after the server went down.
Hrm.
friedo: I don’t believe your statement about javascript having no access to TCP is entirely true… javascript can create standard local library objects (such as XML libraries) which can initiate TCP requests.

We’ve done something like this that works pretty well:


<script>
function serverError() 
{
    // do stuff here
}
</script>

<html>
<img src='http://url.to.tiny.image/on/your/webserver' onerror='javascript:serverError();' style='visibility:hidden;'>
</html>

Like chrisk said, you have to set things up on your server so the image won’t get cached by the browser. Of course, you can’t control this 100% (someone could configure a browser to ignore the server’s cache control directives), so this solution isn’t completely foolproof. But then nothing ever is when you’re talking about a platform where you don’t have control over the client.

Hmm. On preview, I just re-read the question. I’m guessing you don’t have control over the server you’re trying to “ping”. That’s going to make things more difficult, sorry. I’ll leave the above anyway in case it helps somehow.

Now I’m thinking chrisk’s AJAX idea would work better (or more specifically, an XMLHttpRequest which doesn’t necessarily have to get an XML response). That’s probably the way to go. Basically, when you mouseover the link, send an XMLHttpRequest to get the page linked to. If there’s an error, you can let the user know before they click it.

Just out of curiosity, why do you want to do this?

I’ll try the JavaScript approach you provided first. Failing that, I will just have a graphic linked to the server that will only appear if it’s running. I might include instructions to refresh the page to get the current status.

Actually, I do have control over the server, though I rely on a friend of mine to handle the more technical side of things.

Just to save people the hassle of having to deal with a nonresponsive request in case the server is down for some reason.