PDA

View Full Version : Testing connections



philsbury
September 2nd, 2008, 02:40 PM
Hi, is it possible to test a users internet connection using javascript?
Here's the situation, I have an html page which has a form in it. I want to hide the form if the user is not connected to the internet, so that a user can't try and submit the form and finish up with a 'Page cannot be displayed' message.

The reason being is that the project is to go on a CD, so it rules out any server side scripting.

I thought i may be possible to swap the content in, a little like swfObject does this flash.

Anyone any ideas?

actionAction
September 2nd, 2008, 02:56 PM
Can't you just remove the action from the form? That seems like it would be the easiest thing to do. Javascript can't really test for an internet connection, though you can write a function that attempts to load content via ajax and return false if it's unable to load it. Then in your onsubmit for your form you have something like:


<script>
function checkAjax()
{
//try to open a remote file
if(succeed)
return true;
else
return false;
}
</script>
<form action="itwouldbeeasiestjusttoremovethis.php" onsubmit="return checkAjax();">

borrob
September 3rd, 2008, 03:41 AM
you could use an invisible iframe load something like google.com into it and look if it has loaded anything in the onload function of the iframe