• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

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

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
jonnylentilbean
Asked:
jonnylentilbean
  • 2
1 Solution
 
techjoshCommented:
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
 
techjoshCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now