...

View Full Version : How to check for Internet connection



tpeck
09-17-2005, 01:51 AM
Hi. I am trying to think of an elegant way to have a js script check for an Internet connection, and only proceed if one exists. If not, then it displays "Hello World" or whatever I wish to say.

Any ideas? I can only think of trying to visit a site, but I don't know how to write onfail etc.

Thanks,

Terry

Canuck WebGeek
09-17-2005, 03:29 AM
Use an image object to load an image off the internet.
Like so:


function doConnectFunction() {
//I'm fired if I'm connected to the internet
}
function doNoConnectFunction() {
//I'm fired if I'm not connected to the internet
}


var i = new Image();
i.onload = doConnectFunction;
i.onerror = doNoConnectFunction;
i.src = 'http://www.internet.com/image.jpg';

tpeck
09-17-2005, 05:24 AM
I can follow the logic here - thanks, but I can't get it to work!

(Assume the graphic exists - it does.)

I have this page:

*********************

<html>

<head>
<title>INTERNET CONNECT</title>
<script>
function doConnectFunction() {
//I'm fired if I'm connected to the internet
alert("hello");
}
function doNoConnectFunction() {
//I'm fired if I'm not connected to the internet
alert("goodbye");
}
var i = new Image();
i.onload = doConnectFunction;
i.onerror = doNoConnectFunction;
i.src = 'http://www.aapress.com.au/img/71f.gif';
</script>
</head>

<body>
</body>

</html>

***********************

If I am connected to the net, I get the "hello" alert and nothing - which is fine.
But if I am not connected to the net, I get the "hello" alert (fine) but no "goodbye".

What is going on?

Thanks

Canuck WebGeek
09-18-2005, 12:22 AM
If it's always saying hello, then I think the browser my be caching the image, so that it's always loading it.

Try puting the date in the url as a querystring like this:

i.src = 'http://www.aapress.com.au/img/71f.gif?d=' + escape(Date());

That way the request is always unique.

felgall
09-18-2005, 12:26 AM
It could be the browser you are using. Not all browsers trigger an error if the image doesn't download. Some trigger the onload event handler when they give up trying to load the page even if the image didn't download and some don't support these events on images. The code can only be expected to work as required on some versions of IE.

tpeck
09-18-2005, 06:13 AM
I can live with the ie only possibility, but I stillcan't get the page to redirect properly if the connection fails. Instead I get the 404 page error. I now have:

<html>

<head>
<title>INTERNET CONNECT</title>
<script>
function doConnectFunction() {
//I'm fired if I'm connected to the internet
redirURL = "http://www.yahoo.com.au";
self.setTimeout("self.location.href = redirURL;",0000);}
function doNoConnectFunction() {
//I'm fired if I'm not connected to the internet
redirURL = "ifnoconnect.html";
self.setTimeout("self.location.href = redirURL;",0000);}
</script>
<script>
var i = new Image();
i.onload = doConnectFunction;
i.onerror = doNoConnectFunction;
i.src = 'http://www.aapress.com.au/img/71f.gif?d=' + escape(Date());
//escape(Date()) necessary to override possibility of image coming from cache
</script>
</head>

<body>
</body>

</html>

Just can't get that "ifnoconnect.html" page to display.

Thanks for your help.

tpeck
09-18-2005, 06:23 AM
I am stupid. It didn't display because it was being attempted within FrontPage preview - not within the browser.

So it WORKS!

Thanks once again.

Who would have thought such a simple thing as trying to determine if you are connected to the net would be so difficult! Who wrote the javascript rules? Also, I'd much prefer it if the thing didn't have to waste time timing out. Isn't there some registry entry that js can check to see if ie is breezing about online? I guess not. Oh well...

djoxer
08-10-2007, 08:34 PM
I'd like to know this as well.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum