Solved

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

Posted on 2004-10-09
2
156 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XML problem with Internet Explorer 7 45
RSS Feeds--IE 13 178
Snap images to the bottom of a div 5 66
Question on web subscription for apps 3 106
Article by: Matthew
I am a very big proponent of technology compliance standards and strive to meet such criteria in all of my work. That includes my site, which is 100% XHTML 1.0 compliant as determined by the World Wide Web Consortium. https://www.matthewstevenkel…
Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
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…
The viewer will learn how to count occurrences of each item in an array.

776 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