...

View Full Version : Check internetconnection



janne67
01-25-2005, 02:58 PM
Could i check (in javascript) if i have an valid internet connection? I canīt use navigator.onLine because the site is an combination of online/offline site

jbot
01-25-2005, 03:23 PM
the site is a combination of online/offline site

how so?

surely you can still use navigator.online? all you need to do is make sure you handle your condition properly.

joh6nn
01-25-2005, 07:25 PM
you can try to load an image that's located on the server. Image objects have an onerror event handler that fires when they don't load for any reason. in this case, you could do something like the following

<script>
var img = new Image()
img.onerror = function() { alert("Could not connect to the server; please check your network connection"); }
img.src = "http://www.server.com/images/yourimage.gif";
</script>


it's important to note that you have to define the event handler before you assign the image object it's source.

janne67
01-26-2005, 07:09 AM
Thanks i think this solves my problem :) :thumbsup:

DR.Wong
02-10-2005, 04:51 PM
This solves my problem too, I have posted the same question recently, but how do you code it so once you push OK on the alert, it takes you to a new page? :thumbsup:

Brandoe85
02-10-2005, 05:04 PM
<script>
var img = new Image()
img.onerror = function() { alert("Could not connect to the server; please check your network connection"); location.href='http://www.google.com'; }
img.src = "http://www.server.com/images/yourimage.gif";
</script>

?

DR.Wong
02-10-2005, 05:36 PM
That works great, ONE LAST question for utter perfection of this code. If the image loads successfully, can you create an onload function that takes you to another page? :confused: :thumbsup:

DR.Wong
02-10-2005, 05:48 PM
Never mind, I thought it was more complicated,
this is what I did:



<script>
var img = new Image()
onload=window.location="mypage.htm";
img.onerror = function() { alert("Could not connect to the server; please check your network connection"); location.href='http://www.google.com'; }
img.src = "http://www.url.com/img/pic.gif ";
</script>


Thanks for all the help :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum