PDA

View Full Version : validation for connectivity



JShor
04-22-2007, 05:38 AM
Hi,

This will sound silly but I need a script that tells if you are connected to the internet, and if the user is not, then it will show an error message. It is a long story why this script (I know it doesn't make sense) but I am in DIRE need for this script, and I don't know what to do :confused: :confused:

Can anyone help?

Thanks,
-JShor:)

tech_support
04-22-2007, 06:36 AM
You could do this with PHP quite easily.


<?php
if (file_exists('http://www.google.com.au')) {
echo 'It works!';
}
else {
echo 'Error! You cannot view Google!';
}
?>

But I'm not sure if you can do this with JavaScript.

jscheuer1
04-22-2007, 07:24 AM
This works but, you cannot simply navigate to the page unless your cache is disabled or cleared (arrow2.gif or whatever you use there must exist locally):


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<img src="arrow2.gif" onerror="alert('not connected!');">
<script type="text/javascript">
document.images[0].src='http://www.google.com/intl/en_ALL/images/logo.gif';
</script>
</body>
</html>

Or, what you can do though is, once you have navigated to it, hit refresh.

Bob90
04-22-2007, 08:30 AM
Ajax.
Send a request for conformation that you are online.

Cookies.
I'm sure there is a way to test. Maybe use a domain URL check.
IE domain must be "http://www.example.com"


Easily hacked though, but so is the php, unless it writes some encrypted code.

:)

djr33
04-22-2007, 08:40 AM
This is very useful if you have, for example, a slideshow, so you don't keep running it if you're disconnected... it should know, then stop.
Sure, it could be hacked, but this isn't security.... it's functionality.

Twey
04-22-2007, 12:16 PM
<script type="text/javascript">
function checkConnection(yesfunc, nofunc) {
var e = document.createElement("img");
e.onerror = function() {
nofunc();
e = null;
};
e.onload = function() {
yesfunc();
e = null;
};
e.src = "http://www.google.com/images/google_sm.gif?c=" + (new Date().getTime());
}

checkConnection(
function() {
alert("Internet connection succeeded.");
},
function() {
alert("Internet connection failed.");
}
);
</script>This is more reliable than AJAX: there are less reasons it could give a false negative (although there are still some).

jscheuer1
04-22-2007, 01:49 PM
Twey, I had tried something like that at first, using new Date().valueOf(). However, either due to something at Google or a broader problem with the concept, it didn't work. Your code doesn't either. With a remote hosted image on a different server (preferably one that you control), it could work.

In any case, the code I eventually came up with works as I described in my post. It may be able to be improved so as to still use the ubiquitous Google logo while not requiring user intervention to refresh the page.

I wonder though, how the folks at Google feel about something like this?

Twey
04-22-2007, 02:29 PM
Twey, I had tried something like that at first, using new Date().valueOf(). However, either due to something at Google or a broader problem with the concept, it didn't work. Your code doesn't either. With a remote hosted image on a different server (preferably one that you control), it could work."You" as in me, or "you" as in the OP? I was trying to avoid depending upon a remote service that might change or be discontinued in the future. This might work if the OP has a server with the appropriate facilities.
I wonder though, how the folks at Google feel about something like this?I doubt they'd mind terribly, if they even noticed it. It would probably account for something like one in a billion of their requests, even if it were to be checked regularly. In any case, the Google logo was just an example of an image I thought was reasonably likely to stay there for, if not the life of the Internet, a darned long time.

jscheuer1
04-22-2007, 02:38 PM
I meant you as in anybody trying to do this. I subsequently tested this concept on an image on a site I master and it worked fine, as I suspected that it would.

Apparently Google has something going on that prevents it from working with their images. Most likely, whatever that is isn't intended to break our scripts, it just does so as a side effect. Other servers over which one has inadequate control could also break a script like these, intentionally or otherwise.

Bob90
04-23-2007, 09:36 AM
Doesn't


if(document.location.toString().lastIndexOf(/http/) > 0)
{online}
else
{offline}



Quicker

jscheuer1
04-23-2007, 01:08 PM
Doesn't


if(document.location.toString().lastIndexOf(/http/) > 0)
{online}
else
{offline}



Quicker

How does that determine if you are connected to the internet or not?

Bob90
04-23-2007, 01:36 PM
Ok, it should be:

if(document.location.toString().lastIndexOf("http") == 0)
{online}
else
{offline}

When online, location = http://

When saved offline, location = file:///

Thats what they asked for originally.

:)

jscheuer1
04-23-2007, 01:57 PM
It depends upon if the test page is intended to be live or local. Twey and I were assuming local. I think you are assuming live. But even live, there are still problems with your approach. I thought they wanted to know if the computer was connected, not where the page resides. Anyways, when I am viewing pages offline, as long as my ISP and router are up and I haven't disabled the wireless, my computer is still connected to the internet. Also, since this page would have to be local, in order to be even used when the computer was offline, your test would always report offline. If it were a live page, it could be shown offline, along with its address having http in it - as long as it was cached. Additionally a live page like so:


http://www.some.com/http_info.htm

would fail your online test. Oh, and though it doesn't seem to matter here:


file://localhost/

generally isn't used by IE in the address bar and consequently is not in the location property for a local page. Also not crucial - location.href is already a string and would be better than document.location.

Bob90
04-23-2007, 04:00 PM
Ajax request like I said earlier?

:)

jscheuer1
04-23-2007, 04:11 PM
Ajax request like I said earlier?

:)

That has problems because a local page generally cannot retrieve a remote page anyway, though there are exceptions. And, the syntax, assuming that my first objection could be worked out, is far more complex than testing for an image which, will work if the image is a live one that you have sufficient control over to ensure that its query string will be treated normally and that it will be there at least whenever the server it is hosted on is up.

JShor
04-28-2007, 07:24 PM
Hi, Thanks to everyone who posted. PHP is not supported on the machine I am working on, but it would work if I was. Both Bob and Twey's scripts will work, I will use Twey's for now. Thanks again for the help!