James Johnston
James Johnston

Reputation: 9492

Redirect browser only if a web site is available

I have an HTML file stored on the local file system. I need it to redirect (or otherwise display in some fashion) a remote web site ONLY if the site is online and available. If the site is not available, I need to display a user-friendly message.

Currently I have:

<html>
<body onload="window.location.href='http://someserver/';">
    <p>Connecting to remote server...</p>
</body>
</html>

The problem is that if the server is unavailable, the user is presented with an ugly "can't find server" page from the web browser. I would rather display some custom HTML indicating the situation to the user if the page navigation is having trouble. What are some commonly-used solutions I should try that would work well for this?

Requirement: must work in IE6 thru IE9. FireFox/WebKit engines won't be used for this application.

Upvotes: 3

Views: 3190

Answers (2)

RealHowTo
RealHowTo

Reputation: 35372

A simple way will be to try to load a known small image and you detect if the loading is done or not.

<img src="http://www.rgagnon.com/images/pdf.gif"
   onload="window.location='http://www.rgagnon.com';"
   onerror="window.location='http://www.google.com';"
>

If the pdf.gif is loaded, we switch to the site, if not we go somewhere else.

Upvotes: 7

Rob Raisch
Rob Raisch

Reputation: 17357

You could use an AJAX style request on load to assure the server is alive before redirecting the browser to it, but there is no guarantee the server will still be alive when the redirection occurs.

Upvotes: 2

Related Questions