INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Network

How do I detect whether my computer is online or offline? by BillyRayPreachersSon
Posted: 5 May 07 (Edited 9 Apr 12)


Here's a quick way to check whether your computer is online or offline. It's a modified verion of my FAQ216-5781: Detecting the dimensions of an image client-side.

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


Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page @ Code Couch
http://www.codecouch.com/dan/

 

Back to Javascript FAQ Index
Back to Javascript Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close