Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I detect whether my computer is online or offline?

Network

How do I detect whether my computer is online or offline?

by  BillyRayPreachersSon  Posted    (Edited  )

Here's a quick way to check whether your computer is online or offline. It's a modified verion of my faq216-5781.

It has been tested under Windows on IE 6+, Fx 1.0+, Opera 7.02+, and under Mac OS X on Fx 1.07+, IE 5.23, Camino 1.0+, and Opera 8.02+. The code did not work prior to Opera 8.02 - at least in Opera 7.54/Mac, it seems to recognise the offline status, but not the online status.

The original onload / onerror code did not seem to work for me in Safari, however. To fix this, I added a test in the online code to check the image dimensions, which seems to work.

All you need to do is supply the URL of a known image, only available to you when you have an internet connection. I've used the logo image from www.google.co.uk.

Code:
<html>
<head>
	<script type="text/javascript">
	<!--

		var tempImage;

		function checkOnlineStatus() {
			tempImage = new Image();
			tempImage.onload = returnOnlineStatus;
			tempImage.onerror = returnOfflineStatus;
			var imgSrc = 'http://www.google.co.uk/intl/en_uk/images/logo.gif';		// this must point to the url of a valid image.
			tempImage.src = imgSrc + '?randParam=' + new Date().getTime();		// add cache busting
		}

		function returnOnlineStatus() {
			if (tempImage.width == 0 && tempImage.height == 0) {
				returnOfflineStatus();
			} else {
				document.getElementById('onlineStatus').innerHTML = 'You are currently online.';
			}
		}

		function returnOfflineStatus() {
			document.getElementById('onlineStatus').innerHTML = 'You are currently offline.';
		}

	//-->
	</script>
</head>

<body onload="checkOnlineStatus();">
	<div id="onlineStatus">Checking online status, please wait...</div>
</body>
</html>

[!]Please note:[/!] This code is not designed to tell you whether a remote computer is online or offline. It is designed to tell you whether your local computer is online or offline by using the URL of an image that is known to be always available.

To those of you who have contacted me wanting to use this code to monitor remote servers: You really should be using a more bullet-proof solution for server monitoring. Ask a grown-up for help :-/

Dan


[link http://www.coedit.co.uk/][color #00486F]Coedit Limited[/color][/link][color #000000] - Delivering standards compliant, accessible web solutions[/color]

[tt]Dan's Page [blue]@[/blue] Code Couch
http://www.codecouch.com/dan/[/tt]
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top