最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Redirect browser only if a web site is available - Stack Overflow

programmeradmin2浏览0评论

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

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

Share Improve this question asked Jun 2, 2011 at 21:33 James JohnstonJames Johnston 9,4929 gold badges53 silver badges77 bronze badges 3
  • You could do an XHR to the URL and see whether it succeeds. – Pointy Commented Jun 2, 2011 at 21:35
  • When you say 'remote web site' I think that prevents JavaScript from working (due to the same-origin policy). I think you'll need a server-side script (such as php, for example) to implement this. – David Thomas Commented Jun 2, 2011 at 21:36
  • 1 The HTML file is stored in an executable file as a Windows resource. Specificially, it is accessed using a URL like: res://mylibrary.dll/redirector.htm. It's then hosted in a C++ program using the IE web browser control. (I should add/apologize that I have not regularly done anything with HTML/JS for several years... - most of my work is in C++ these days) I should hope/think that the res: protocol gets a fair bit of freedom from a security perspective, seeing as how this is the same protocol that IE uses internally for error pages, etc... – James Johnston Commented Jun 2, 2011 at 21:41
Add a ment  | 

2 Answers 2

Reset to default 7

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./images/pdf.gif"
   onload="window.location='http://www.rgagnon.';"
   onerror="window.location='http://www.google.';"
>

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

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.

发布评论

评论列表(0)

  1. 暂无评论