[Zope] Ugly JS hacks incorporated: Making a web page that looks for the server.

Lennart Regebro lennart@regebro.nu
Wed, 30 Jan 2002 16:31:27 +0100


This little script will try to connect to the localhost:8080 server. When it
succeeds it will open http://localhost:8080/ . So, you don't get an error
page if the server isn't started. It will just go on trying until the server
is there, and then open the page.
A script like this will be included in the distribution of our content
management system.

Now, how does this work? It's simple, it's ugly and it's almost funny.
It loads http://localhost:8080/ into an iframe 1x1 pixels big, and then
waits 5 seconds.
Then it checks if the iframes location.protocol is http. And if it isn't, I
try again.

The thing is, that when IE5.5 succeeds in loading the iframe, the iframes
location *does not have a protocol property*.
Hence, if the fram *is* loaded, the test will throw an exception, in which
case I open the page.

*Tada!*

I feel like sharing this horrible ugliness. Here you are:

<html>
<head>
<eta http-equiv="Refresh" CONTENT="5">
<title>Locating EasyPublisher Server</title>
</head>
<body>
<p>Waiting for server to start...</p>
<iframe name="newFrame" height="1" width="1">
&nbsp;
</iframe>
<script language="JavaScript">

function check() {
  try {
    if (newFrame.location.protocol != 'http:') {
      newFrame.location = 'http://localhost:8080/';
      setTimeout('check()', 5000);
    }
  } catch(e) {
     document.location = 'http://localhost:8080/';
  }

}

newFrame.location = 'http://localhost:8080/';
setTimeout('check()', 5000);

</script>
</body>
</html>