JS on-/offline switcher


JS on-/offline switcher

08.12.2003, 15:36 · Martin · Permalink

What I'm trying to do:
I have a HTML page on the local (file-)system. If it get's loaded, some JS needs to decide if a (online) address is reacheable, and load that or another local page accordingly...

I'm looking for ideas of hpw to discover if a remote URL is reacheable in JavaScript... maybe by doing some kind of var rimg = new Image(remoteUrl); kind of thing...

I'm open for suggestions on this.

This seems to work ok;
<html>
<head>
<title>On-/Offline Tester </title>
<script language="JavaScript" >
        switchUrl = "file:///C:/locals/index.html";
</script>
<script src="http://remote.tld/switchUrl.js" language="JavaScript" ></script>
</head>
<body onload="location.href=switchUrl;" >
<h1>On-/Offline Tester </h1>
</body>
</htmö>

#### in the switchUrl.js ####
switchUrl = "http://remote.tld/index.html";


No, that's too easy, isn't it? the remote *.js file is CACHED by IE and others!!!

Ah, Danke Andi, so geht's:
var randNum = Math.random ();
document.write("<scr"+"ipt src=\"http://remote.tld/switchUrl.js?"+randNum"\" language=\"JavaScript\"></scri"+"pt>);


Replacing the second script tag with the static *.js URL by an dynamic' URL prevents caching nicely :) <


alles Bild, Text und Tonmaterial ist © Martin Spernau, Verwendung und Reproduktion erfordert die Zustimmung des Authors

Martin Spernau
© 1994-2026


amazon.de Wunschliste

Facebook me!
Google+

powered by Traumtank