Solved

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

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

707 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

12 Experts available now in Live!

Get 1:1 Help Now