How ot test if an internet connection exists...

E

ezmiller

Does anybody know how to use javascript to test whether or not an
internet connection exists? Is this possible even?
 
@

@sh

I presume this page resides on an intranet or local network - in which case
you could perhaps set a Body onLoad to redirect to a webpage, plus set a
Javascript timeout running at the same time. If the internet connection
exists it will redirect the browser to it, if it doesn't exist then the
javascript timeout will redirect the user to a 'non internet connection'
version of the pages?

Not sure if thats even possible, but its an idea...

A better way to be to use a component to ping an internet address and wait
for a reply.

Blabbering a bit and thinking out loud here, but it may give you an idea.

Cheers, Ash
 
B

beholdthepanda

I have a bit of code that will detect what type of connection the user
is - well - using. It can detect 3 states "modem" "lan" and "offline."

THIS ISN'T PURE JS because it uses clientcaps behavior which is only
supported in IE.

For more info on this behavior check out:
http://msdn.microsoft.com/workshop/author/behaviors/reference/properties/connectiontype.asp

A page with this code on it should be able, in IE, to detect how the
user got there (modem, lan or offline connection)

You can pretty much see what's going on in here, you can get creative
anywhere after the bodyObj.style.behavior = "url(#default#clientCaps)";
which is where we store what connection type we get:

//Connection Detection v2
var SSClientCaps_wait_interval_01 = null;
var SSModemFound = null;

function SSClientCapsWaitForDOM() {
// need to wait for document before looking up clientCaps
properties
if (document.readyState != "interactive" && document.readyState
!= "complete")
return;

var bodyObjArray = document.getElementsByTagName("body");
var bodyObj = bodyObjArray[0];
if (!bodyObj)
return;
clearInterval(SSClientCaps_wait_interval_01);
bodyObj.style.behavior = "url(#default#clientCaps)";
if (bodyObj.connectionType!="lan" &&
bodyObj.connectionType!="offline"){
SSModemFound = true;

}
else
{
//nothing.
}
}//end SSClientCapsWaitForDOM()


function SSIntSetter(){
SSClientCaps_wait_interval_01 =
setInterval("SSClientCapsWaitForDOM()", 50);
}//end SSIntSetter

SSIntSetter(); //begins bandwidth detection
 
R

Randy Webb

(e-mail address removed) said the following on 12/16/2005 2:39 PM:

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.
I have a bit of code that will detect what type of connection the user
is - well - using. It can detect 3 states "modem" "lan" and "offline."

For me it detects nothing.

THIS ISN'T PURE JS because it uses clientcaps behavior which is only
supported in IE.

For more info on this behavior check out:
http://msdn.microsoft.com/workshop/author/behaviors/reference/properties/connectiontype.asp

A page with this code on it should be able, in IE, to detect how the
user got there (modem, lan or offline connection)

But it didn't.
You can pretty much see what's going on in here, you can get creative
anywhere after the bodyObj.style.behavior = "url(#default#clientCaps)";
which is where we store what connection type we get:


alert('It didnt work')
//nothing.
}
}//end SSClientCapsWaitForDOM()

<snip>

Adding the alert into the else branch for testing, no matter how I
opened the page. Locally, offline, online, from a server, in IE6 XP SP2,
I get the "It didnt work" alert. Maybe you might try testing your code
again.
 
B

beholdthepanda

Thanks Randy,

I just put this very simple web page together and sure, enough on
XP/SP2 IE6 I get a 'lan'
in my IE status bar. (lower/left of browser). Check it out. Also be
weary of any funky line breaks you may get from copying / pasting.
This was included in Script block inside the page's <BODY> tag. Hope
you get positive results out of it.


//Connection Detection v2
var SSClientCaps_wait_interval_01 = null;
var SSModemFound = null;


function SSClientCapsWaitForDOM() {
// need to wait for document before looking up clientCaps
properties
if (document.readyState != "interactive" && document.readyState
!= "complete")
return;

var bodyObjArray = document.getElementsByTagName("body");
var bodyObj = bodyObjArray[0];
if (!bodyObj)
return;
clearInterval(SSClientCaps_wait_interval_01);
bodyObj.style.behavior = "url(#default#clientCaps)";
window.status = bodyObj.connectionType;

if (bodyObj.connectionType!="lan" &&
bodyObj.connectionType!="offline"){
SSModemFound = true;


}
else
{
//nothing.

}
}//end SSClientCapsWaitForDOM()


function SSIntSetter(){
SSClientCaps_wait_interval_01 =
setInterval("SSClientCapsWaitForDOM()", 50);
}//end SSIntSetter


SSIntSetter(); //begins bandwidth detection



</Script>
=======================================
 
R

Randy Webb

Jasen Betts said the following on 12/17/2005 2:11 AM:
try to dowbnload something (eg an img) and wait for the onload to succeed.

Doesn't do what the OP needs to do.
 
R

Randy Webb

Jasen Betts said the following on 12/18/2005 3:04 AM:
how is that?

Because it doesn't tell you whether an internet connection exists or
not. It only tells you if the resource was downloaded or not. An image
is the worst resource to try to test that with. And if the resource
downloads, that may or may not indicate a connection based on cache
settings. If the resource does not download, that does not mean a
connection isn't present, it means the resource didn't download. They
are not the same thing.
 
R

Randy Webb

Jasen Betts said the following on 12/20/2005 4:39 AM:
what's an internet connection ? :^)

Two tin cans and some thread to connect me to the world? said:
unique it name by tacking a query onto its name

AOL is very well known to me to not honor that request. Even when cache
headers are set properly. But it's a start when using a decent
browser/service.
true it means either there's no connection or there's a problem with the
server, in which case a connection is probably useless anyway.

Not necessarily the server though. A simple typo in the name of the
image can prevent the image from downloading but everything else will
work properly. It only indicates a problem, it can not explicitly
determine that problem.
In many cases they are functionally equivalent

Read above.
 
V

VK

ezmiller said:
Does anybody know how to use javascript to test whether or not an
internet connection exists? Is this possible even?

Yes of course, presuming (rather reasonnably) that you are running your
script from local machine (so free from same-domain limits) and you
want to check if it is connected to the Internet.

Take an AJAX module like <http://www.ajaxtoolbox.com> and use it to
request say three front pages of well known sites, say:
Google.com
Yahoo.com
MSN.com

If all three of them give you error then:
1) You are not connected.
or
2) The end of the world came, and it is not important then: are you
connected or not.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top