473,385 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How ot test if an internet connection exists...

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

Dec 15 '05 #1
15 27743
@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
"ezmiller" <et*************@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Does anybody know how to use javascript to test whether or not an
internet connection exists? Is this possible even?

Dec 15 '05 #2
ezmiller wrote on 15 dec 2005 in comp.lang.javascript:
Does anybody know how to use javascript to test whether or not an
internet connection exists? Is this possible even?

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

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

Dec 15 '05 #3
ezmiller wrote:
Does anybody know how to use javascript to test whether or not an
internet connection exists? Is this possible even?


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
Dec 15 '05 #4
RobG said the following on 12/15/2005 9:21 AM:
ezmiller wrote:
Does anybody know how to use javascript to test whether or not an
internet connection exists? Is this possible even?


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 >


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/
Dec 15 '05 #5
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

Dec 16 '05 #6
be************@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.
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/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)
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:

<snip>
else
{
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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 16 '05 #7
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:
But it didn't. It should now. be************@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.
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/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)


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:


<snip>
else
{


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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


Dec 17 '05 #8
On 2005-12-15, ezmiller <et*************@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 dowbnload something (eg an img) and wait for the onload to succeed.

Bye.
Jasen
Dec 17 '05 #9
Jasen Betts said the following on 12/17/2005 2:11 AM:
On 2005-12-15, ezmiller <et*************@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 dowbnload something (eg an img) and wait for the onload to succeed.


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/
Dec 17 '05 #10
On 2005-12-17, Randy Webb <Hi************@aol.com> wrote:
Jasen Betts said the following on 12/17/2005 2:11 AM:
On 2005-12-15, ezmiller <et*************@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?

Bye.
Jasen
Dec 18 '05 #11
Jasen Betts said the following on 12/18/2005 3:04 AM:
On 2005-12-17, Randy Webb <Hi************@aol.com> wrote:
Jasen Betts said the following on 12/17/2005 2:11 AM:
On 2005-12-15, ezmiller <et*************@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?


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/
Dec 18 '05 #12
On 2005-12-18, Randy Webb <Hi************@aol.com> wrote:
Jasen Betts said the following on 12/18/2005 3:04 AM:
On 2005-12-17, Randy Webb <Hi************@aol.com> wrote:
Jasen Betts said the following on 12/17/2005 2:11 AM:

On 2005-12-15, ezmiller <et*************@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?


Because it doesn't tell you whether an internet connection exists or
not.


what's an internet connection ? :^)
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,
unique it name by tacking a query onto its name
that does not mean a connection isn't present,
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.
it means the resource didn't download. They
are not the same thing.


In many cases they are functionally equivalent

Bye.
Jasen
Dec 20 '05 #13
Jasen Betts said the following on 12/20/2005 4:39 AM:
On 2005-12-18, Randy Webb <Hi************@aol.com> wrote:
Jasen Betts said the following on 12/18/2005 3:04 AM:
On 2005-12-17, Randy Webb <Hi************@aol.com> wrote:
Jasen Betts said the following on 12/17/2005 2:11 AM:
>On 2005-12-15, ezmiller <et*************@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?
Because it doesn't tell you whether an internet connection exists or
not.

what's an internet connection ? :^)


Two tin cans and some thread to connect me to the world? <g>
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,

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.
that does not mean a connection isn't present,

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.
it means the resource didn't download. They
are not the same thing.

In many cases they are functionally equivalent


Read above.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 20 '05 #14
VK
ezmiller wrote:
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.

Dec 20 '05 #15
On 2005-12-20, Randy Webb <Hi************@aol.com> wrote:
Jasen Betts said the following on 12/20/2005 4:39 AM:
On 2005-12-18, Randy Webb <Hi************@aol.com> wrote:
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,

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.


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...
that does not mean a connection isn't present,


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.


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

Bye.
Jasen
Dec 21 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: m004202002 | last post by:
I recently visited a site http://us.mcafee.com/root/speedometer/default.asp which find out the connection speed . Can any body explain how(logic) to do it ? Can i do that using php ? Please help...
8
by: Jozef | last post by:
Hello, Is there a way to test that an internet connection exists and that you are able to see the webserver before performing any connections? The reason being, when I try to connect to an SQL...
3
by: Jonny | last post by:
Hi, Please could you tell me how to check for an internet connection in C. I'm using Windows 2000. Many Thanks, Jonny
4
by: Goldwind | last post by:
Hi, From a desktop application, i want to check if an internet connection exist. Many articles use the win api "InternetGetConnectedState", but it is no sure thing. I can check it by calling a...
4
by: John Riddle | last post by:
Hello, I have an application that runs continously gives an error and stops working if the internet goes down (which happens for about 5-10 minutes several times a day. Can anybody tell me how...
4
by: adalton | last post by:
I've been trying to figure out how to test for a valid internet connection. I am able to connect to a valid internet connection, but when it drops, or if it is not there when my program starts, I...
6
by: =?Utf-8?B?QnNtZW5nZW4=?= | last post by:
I am trying to make sure that the local connection is up. I have presently been using the NetworkChange.NetworkAvailabilityChanged Event for this. Is there a better way to do this? Also, I...
0
Ali Rizwan
by: Ali Rizwan | last post by:
Hi all, I m facing a big problem... How can i detect that either the internet connection id dialup or is on LAN or is their both connection exists...... and what is the connection speed. And is...
11
by: Alexnb | last post by:
Hello internet. I am wondering, is there a simple way to test for Internet connection? If not, what is the hard way :p -- View this message in context:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.