Connecting Tech Pros Worldwide Forums | Help | Site Map

How ot test if an internet connection exists...

ezmiller
Guest
 
Posts: n/a
#1: Dec 15 '05
Does anybody know how to use javascript to test whether or not an
internet connection exists? Is this possible even?

@sh
Guest
 
Posts: n/a
#2: Dec 15 '05

re: How ot test if an internet connection exists...


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


"ezmiller" <ethanzanemiller@gmail.com> wrote in message
news:1134655990.360033.126810@f14g2000cwb.googlegr oups.com...[color=blue]
> Does anybody know how to use javascript to test whether or not an
> internet connection exists? Is this possible even?
>[/color]


Evertjan.
Guest
 
Posts: n/a
#3: Dec 15 '05

re: How ot test if an internet connection exists...


ezmiller wrote on 15 dec 2005 in comp.lang.javascript:
[color=blue]
> Does anybody know how to use javascript to test whether or not an
> internet connection exists? Is this possible even?
>[/color]

<img src='http://myDomain.org/images/test1x1px.gif'
onerror='alert("no connection")'
onload='alert("CONNECTED")'[color=blue]
>[/color]


--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

RobG
Guest
 
Posts: n/a
#4: Dec 15 '05

re: How ot test if an internet connection exists...


ezmiller wrote:[color=blue]
> Does anybody know how to use javascript to test whether or not an
> internet connection exists? Is this possible even?
>[/color]

You could use XMLHttpRequest to see if some resource that you know of on
the internet is available. Not 100% perfect, but should do the trick.

<URL: http://jibbering.com/2002/4/httprequest.html >


--
Rob
Randy Webb
Guest
 
Posts: n/a
#5: Dec 15 '05

re: How ot test if an internet connection exists...


RobG said the following on 12/15/2005 9:21 AM:[color=blue]
> ezmiller wrote:
>[color=green]
>> Does anybody know how to use javascript to test whether or not an
>> internet connection exists? Is this possible even?
>>[/color]
>
> You could use XMLHttpRequest to see if some resource that you know of on
> the internet is available. Not 100% perfect, but should do the trick.
>
> <URL: http://jibbering.com/2002/4/httprequest.html >[/color]

Not if the browser is setup to connect automatically when a web request
is made though :)

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
beholdthepanda@gmail.com
Guest
 
Posts: n/a
#6: Dec 16 '05

re: How ot test if an internet connection exists...


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/a...ectiontype.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

Randy Webb
Guest
 
Posts: n/a
#7: Dec 16 '05

re: How ot test if an internet connection exists...


beholdthepanda@gmail.com 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.
[color=blue]
> 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."[/color]

For me it detects nothing.

[color=blue]
> 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/a...ectiontype.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)[/color]

But it didn't.
[color=blue]
> 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:
>[/color]

<snip>
[color=blue]
> else
> {[/color]

alert('It didnt work')
[color=blue]
> //nothing.
> }
> }//end SSClientCapsWaitForDOM()
>[/color]

<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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
beholdthepanda@gmail.com
Guest
 
Posts: n/a
#8: Dec 17 '05

re: How ot test if an internet connection exists...


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>
=======================================



Randy Webb wrote:[color=blue]
> But it didn't.[/color]
It should now.[color=blue]
> beholdthepanda@gmail.com 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.
>[color=green]
> > 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."[/color]
>
> For me it detects nothing.
>
>[color=green]
> > 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/a...ectiontype.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)[/color]
>
> But it didn't.
>[color=green]
> > 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:
> >[/color]
>
> <snip>
>[color=green]
> > else
> > {[/color]
>
> alert('It didnt work')
>[color=green]
> > //nothing.
> > }
> > }//end SSClientCapsWaitForDOM()
> >[/color]
>
> <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.
>
> --
> Randy
> comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
> Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/[/color]

Jasen Betts
Guest
 
Posts: n/a
#9: Dec 17 '05

re: How ot test if an internet connection exists...


On 2005-12-15, ezmiller <ethanzanemiller@gmail.com> wrote:[color=blue]
> Does anybody know how to use javascript to test whether or not an
> internet connection exists? Is this possible even?[/color]

try to dowbnload something (eg an img) and wait for the onload to succeed.

Bye.
Jasen
Randy Webb
Guest
 
Posts: n/a
#10: Dec 17 '05

re: How ot test if an internet connection exists...


Jasen Betts said the following on 12/17/2005 2:11 AM:[color=blue]
> On 2005-12-15, ezmiller <ethanzanemiller@gmail.com> wrote:
>[color=green]
>>Does anybody know how to use javascript to test whether or not an
>>internet connection exists? Is this possible even?[/color]
>
>
> try to dowbnload something (eg an img) and wait for the onload to succeed.[/color]

Doesn't do what the OP needs to do.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jasen Betts
Guest
 
Posts: n/a
#11: Dec 18 '05

re: How ot test if an internet connection exists...


On 2005-12-17, Randy Webb <HikksNotAtHome@aol.com> wrote:[color=blue]
> Jasen Betts said the following on 12/17/2005 2:11 AM:[color=green]
>> On 2005-12-15, ezmiller <ethanzanemiller@gmail.com> wrote:
>>[color=darkred]
>>>Does anybody know how to use javascript to test whether or not an
>>>internet connection exists? Is this possible even?[/color]
>>
>> try to download something (eg an img) and wait for the onload to succeed.[/color]
>
> Doesn't do what the OP needs to do.[/color]

how is that?

Bye.
Jasen
Randy Webb
Guest
 
Posts: n/a
#12: Dec 18 '05

re: How ot test if an internet connection exists...


Jasen Betts said the following on 12/18/2005 3:04 AM:[color=blue]
> On 2005-12-17, Randy Webb <HikksNotAtHome@aol.com> wrote:
>[color=green]
>>Jasen Betts said the following on 12/17/2005 2:11 AM:
>>[color=darkred]
>>>On 2005-12-15, ezmiller <ethanzanemiller@gmail.com> wrote:
>>>
>>>
>>>>Does anybody know how to use javascript to test whether or not an
>>>>internet connection exists? Is this possible even?
>>>
>>>try to download something (eg an img) and wait for the onload to succeed.[/color]
>>
>>Doesn't do what the OP needs to do.[/color]
>
>
> how is that?[/color]

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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jasen Betts
Guest
 
Posts: n/a
#13: Dec 20 '05

re: How ot test if an internet connection exists...


On 2005-12-18, Randy Webb <HikksNotAtHome@aol.com> wrote:[color=blue]
> Jasen Betts said the following on 12/18/2005 3:04 AM:[color=green]
>> On 2005-12-17, Randy Webb <HikksNotAtHome@aol.com> wrote:
>>[color=darkred]
>>>Jasen Betts said the following on 12/17/2005 2:11 AM:
>>>
>>>>On 2005-12-15, ezmiller <ethanzanemiller@gmail.com> wrote:
>>>>
>>>>
>>>>>Does anybody know how to use javascript to test whether or not an
>>>>>internet connection exists? Is this possible even?
>>>>
>>>>try to download something (eg an img) and wait for the onload to succeed.
>>>
>>>Doesn't do what the OP needs to do.[/color]
>>
>>
>> how is that?[/color]
>
> Because it doesn't tell you whether an internet connection exists or
> not.[/color]

what's an internet connection ? :^)
[color=blue]
> 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,[/color]

unique it name by tacking a query onto its name
[color=blue]
> that does not mean a connection isn't present,[/color]

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.
[color=blue]
> it means the resource didn't download. They
> are not the same thing.[/color]

In many cases they are functionally equivalent

Bye.
Jasen
Randy Webb
Guest
 
Posts: n/a
#14: Dec 20 '05

re: How ot test if an internet connection exists...


Jasen Betts said the following on 12/20/2005 4:39 AM:[color=blue]
> On 2005-12-18, Randy Webb <HikksNotAtHome@aol.com> wrote:
>[color=green]
>>Jasen Betts said the following on 12/18/2005 3:04 AM:
>>[color=darkred]
>>>On 2005-12-17, Randy Webb <HikksNotAtHome@aol.com> wrote:
>>>
>>>
>>>>Jasen Betts said the following on 12/17/2005 2:11 AM:
>>>>
>>>>
>>>>>On 2005-12-15, ezmiller <ethanzanemiller@gmail.com> wrote:
>>>>>
>>>>>
>>>>>
>>>>>>Does anybody know how to use javascript to test whether or not an
>>>>>>internet connection exists? Is this possible even?
>>>>>
>>>>>try to download something (eg an img) and wait for the onload to succeed.
>>>>
>>>>Doesn't do what the OP needs to do.
>>>
>>>
>>>how is that?[/color]
>>
>>Because it doesn't tell you whether an internet connection exists or
>>not.[/color]
>
>
> what's an internet connection ? :^)
>[/color]

Two tin cans and some thread to connect me to the world? <g>
[color=blue]
>[color=green]
>>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,[/color]
>
>
> unique it name by tacking a query onto its name[/color]

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.
[color=blue]
>[color=green]
>>that does not mean a connection isn't present,[/color]
>
>
> 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.[/color]

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.
[color=blue]
>[color=green]
>>it means the resource didn't download. They
>>are not the same thing.[/color]
>
>
> In many cases they are functionally equivalent[/color]

Read above.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
VK
Guest
 
Posts: n/a
#15: Dec 20 '05

re: How ot test if an internet connection exists...


ezmiller wrote:[color=blue]
> Does anybody know how to use javascript to test whether or not an
> internet connection exists? Is this possible even?[/color]

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.

Jasen Betts
Guest
 
Posts: n/a
#16: Dec 21 '05

re: How ot test if an internet connection exists...


On 2005-12-20, Randy Webb <HikksNotAtHome@aol.com> wrote:[color=blue]
> Jasen Betts said the following on 12/20/2005 4:39 AM:[color=green]
>> On 2005-12-18, Randy Webb <HikksNotAtHome@aol.com> wrote:
>>[color=darkred]
>>>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,[/color]
>>
>>
>> unique it name by tacking a query onto its name[/color]
>
> 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.[/color]

if AOL responds then then the internet connection is present.
if verification from the server is needed unique the file name itself and
serve it from a CGI. (that should fool AOL) or serve it from an unusual port
number...
[color=blue][color=green]
>>[color=darkred]
>>>that does not mean a connection isn't present,[/color]
>>
>> 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.[/color]
>
> 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.[/color]

broken code is broken code, and with any method chosen is always a risk.

Bye.
Jasen
Closed Thread


Similar JavaScript / Ajax / DHTML bytes