Solved

Detect presense of dial-up internet connection in Javascript/DHTML/HTML

Posted on 2004-10-09
2
154 Views
Last Modified: 2010-04-06
I'm trying to revamp my local homepage to incorporate an automatic option that detects the presence of an internet connection. In the case of an internet connection, the browser redirects to an internet website [in this case, gmail.com], and otherwise, it loads a page local to my computer.

Though i'm farirly javascript and html literate [assuming this is possible], a semi-lamen's termed walkthrough would be greatly appreciated. I really hope this is possible. Thanks.
0
Comment
Question by:jonnylentilbean
  • 2
2 Comments
 
LVL 3

Accepted Solution

by:
techjosh earned 125 total points
ID: 12426454
So, heres how it works... you set the imgsrc variable to an image that is always available when you are online.
You set the online_url to the url you want to go to if you are online and the offline_url to the url you want to go to if you're offline.

The javascript Image() object tries to download the image at imgsrc... if it can it runs the onload function... if it cant it runs the onerror function... if you cancel the page load it runs the onabort function... onabort and onerror both execute the offline function while onload executes the online function... hope its not too cryptic.

Also, if your browser caches the image it may report itself being online when you are really offline.

<html>
<head>
<title>Online test</title>
<script language=javascript>
<!--
var imgsrc = "http://www.google.com/images/logo_sm.gif";
var online_url = "http://www.google.com";
var offline_url = "c:\\offline.htm";

function TestConnection()
{
      var img = new Image();
      img.onabort = Offline;
      img.onerror = Offline;
      img.onload = Online;

      img.src = imgsrc;
}

function Online()
{
      alert ("You are online!");
      document.location.href = online_url;
}

function Offline()
{
      alert ("You are offline!");
      document.location.href = offline_url;
}

//-->
</script>
</head>
<body onload="TestConnection();">
<H1>Please Wait...</H1>
</body>
</html>
0
 
LVL 3

Expert Comment

by:techjosh
ID: 12427755
If you have a problem with your browser caching the image so that the page always says it's online you need to find an image that isn't cached... lots of dynamic images are like this.

try this image if you have this problem:
var imgsrc = "http://www.danasoft.com/vipersig.jpg";

If that fixes your problem you probably want to find another dynamic image as it might be considered rude to use his image and his bandwidth to see if you are online.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Owl Carousel 6 173
xml xsd validation free tool to download 10 76
Coldfusion- Create and save form elements in Database 7 61
Windows Tool to Build Android and iOS App 3 54
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now