0

I'm trying to check if browser has internet connection with javascript but I encountered some problems on IE 5.5

<script>
function checkConnection(){
           if(navigator.onLine === false){
               //document.execCommand("Stop");
               alert("No internet connection.");
               document.execCommand("Stop");
}
</script>

and:

<input type="submit" value="GO" name="whereTo" onclick="checkConnection();"  />

It seems that IE 5.5 doesn't have navigator.onLine property, how can I check for connection for IE 5.5?

Spudley
  • 166,037
  • 39
  • 233
  • 307
hitech
  • 9
  • 4
  • You shouldn't want to be dealing with such outdated stuff I guess... – CaptainCarl Nov 08 '13 at 08:35
  • 3
    IE 5.5? That was released over 13 years ago! Do you *really* need to support it? – lonesomeday Nov 08 '13 at 08:35
  • 3
    The internet was not yet invented in the time of Internet Explorer 5.5 – Freeman Lambda Nov 08 '13 at 08:36
  • Yes because customer has PDA which runs only IE 5.5 unfortunately EDIT: Let me put it this way: i want to check at least network connection, it is not necessary to put it on internet :) – hitech Nov 08 '13 at 08:36
  • @user2902058 What are you going to do if they are online and if they are offline? (I doubt IE5.5 would support it anyway.) – lonesomeday Nov 08 '13 at 08:39
  • 2
    IE5.5 is like a wide open door to viruses and worms come in... – Teemu Nov 08 '13 at 08:39
  • 1
    Tell them to get a new phone? – ggdx Nov 08 '13 at 08:41
  • Come on guys i know that this is bad case, but i'm trying to solve a problem anyway – hitech Nov 08 '13 at 08:43
  • @user2902058 Can you clarify *why* you want to test this? – lonesomeday Nov 08 '13 at 09:18
  • I'm developing web application and in case wifi drops, you get message thad you don't havce connection – hitech Nov 08 '13 at 09:22
  • Have yourself some respect and drop IE5.5 support. First you need to learn to let go... – lukas.pukenis Nov 08 '13 at 09:55
  • "*customer has PDA which runs only IE 5.5*" ... That's fine. Actually, if you've got someone paying you to work with IE5.5, then fair enough. However, it is important for both you and your customer to understand that IE5.5 is not supported and has not been for some time. Features you rely on may simply not exist. There's nothing wrong with accepting a job working on obsolete tech, but you should make sure you bill your time appropriately (ie charge him a lot more than normal), and make sure that expectations are not set too high on what you will be able to achieve. – Spudley Nov 08 '13 at 10:13

1 Answers1

2

Why not trying to send an AJAX request? It won't check if you're strictly online, but it would tell you if you can reach something... Trying a couple of URLs might be enough...

function ajaxRequest(url) {
  var xmlhttp;
  if (window.XMLHttpRequest) {
    // code for IE 7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  }  else {
    // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState !== 4 && xmlhttp.status !== 200) {
      alert("No internet connection.");
      document.execCommand("Stop");
    }
  }

  xmlhttp.open("GET",url, true);
  xmlhttp.send();
  return false;
}
MarcoL
  • 9,829
  • 3
  • 37
  • 50