Closed Thread Icon

Topic awaiting preservation: How to detect an internet connection? (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8581" title="Pages that link to Topic awaiting preservation: How to detect an internet connection? (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: How to detect an internet connection? <span class="small">(Page 1 of 1)</span>\

 
Sam
Bipolar (III) Inmate

From: Belgium
Insane since: Oct 2002

posted posted 03-24-2003 10:57

Hi all,

I started a topic in server side scripting which results in this javascript question.

How do you detect inside a HTML page whether or not the page is called online or offline (=IMG tags don't work so I don't want to display the pictures)?

Thanks,
Sam

Nevel
Bipolar (III) Inmate

From: Amsterdam
Insane since: Jun 2002

posted posted 03-24-2003 15:38

Fun question. Got a solution, too:

<html>
<head>
<script type="text/javascript">
function whatever(online){
var onlineTxt = online ? 'online' : 'offline';
alert('this mail is viewed ' + onlineTxt);
}
</script>
</head>
<body>
<img src="http://www.blaat.com/gfx/bladibla.gif" onload="whatever(1)" onerror="whatever(0)" style="visibility:hidden"/>
</body>
</html>

The image inside your body-tag is just a test-image. Make sure the image exists, but because it's just a test-img, make sure it is little(1x1pixel or something like that).
If the user can view the image, onload is being fired. If he can't, onerror wil be fired. Both events are calling the same function, which then "knows" whether the user is online or not. From there on, it's up to you.

Good luck,

Nevel

Sam
Bipolar (III) Inmate

From: Belgium
Insane since: Oct 2002

posted posted 03-24-2003 16:11

Nevel, Thank you very much!




[This message has been edited by Sam (edited 03-24-2003).]

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 03-24-2003 18:09

Beware (be very ware): this probably won't work in any browser but IE. I don't know for sure, but it just *sounds* like an IE-only feature.

Nevel
Bipolar (III) Inmate

From: Amsterdam
Insane since: Jun 2002

posted posted 03-24-2003 22:00

Sorry Slime,

As far as I can see, it works fine in Opera and NS7 too.
Weird thing is, the more elegant way(myImg = new Image(); etc) doesn't seem to work at all :s.

But I don't see which feature you were doubting, actually. As long as the onload and the onabort events work, it should be cross-browser, aight?

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 03-24-2003 22:15

Ooooh, whoops, I was misreading the code. I thought "online" was some sort of predefined global variable.

Dracusis
Maniac (V) Inmate

From: Brisbane, Australia
Insane since: Apr 2001

posted posted 03-26-2003 00:49

Will this work if the image is cached?

I've had all sorts of issues using the onload=() handeler in image elements before. I first used them when writing a very very crude image preload script but I discovered that sometimes that event wouldn't fire if the image was already in the browsers cache hence it didn't need to "load" it and the onLoad event didn't fire.

As for myImage = new Image()...
When you do this your creating a new Image() Object. Even if you point it's obj.src property to a location that you can't acess (if the user is offline) it will still create the image object it just won't be able to load any image data into it. Maybe you could check: myOnlineStatus = (myImageObject.loaded)? "online" : "offline"; that might be a better way of doing this as you won't have any issues when the load event is being fired (or not), as I tihink the obj.loaded property checks against the browsers cache in the first place.

You'll also want to check this after a given ammount of time has passed after the document.onLoad event. It you check this as the page is loading chances are it will always show as offline as you need to give the image time to load. To speed this up create the image object in a script in the head (outside of a function) and just use a 1x1 pixel gif image to test against.

« BackwardsOnwards »

Show Forum Drop Down Menu