1

Possible Duplicate:
Check if Internet Connection Exists with Javascript?

I have been working on a manual to be sent via CD to a customer. The owner of my company requires that the manual be split into many subsections as .doc and .docx files. The table of contents for this manual will be an HTML page, to be run by autorun when the CD is put into the computer.

I can be fairly sure that the computer is a windows computer, but that's about it. My current thinking is to use this script to take advantage of google to display the word file, but only if the computer is actually connected to the internet. That way I don't have to rely on the customer having a particular version of word (or having a compatibility pack installed).

From the research I've done, relying on a computer to correctly recognize that a link to a word document should be opened with word is problematic, and has to be resolved at the user end.

Community
  • 1
  • 1
Brian S
  • 19
  • 1
  • I believe a `pdf` would be more suited for this. – kei Jul 07 '11 at 19:33
  • 2
    Can't you just convert the files to pdf / odf? – PeeHaa Jul 07 '11 at 19:34
  • If you really want to display the document with no software requirements on the end-user end other than a browser, then the content should all be HTML on the CD. You could even offer the user a choice of Word, PDF or HTML. – jfriend00 Jul 07 '11 at 19:40
  • Keep in mind that if you use Google Docs Viewer then you also agree to their TOS - https://docs.google.com/viewer/TOS?hl=en – mrtsherman Jul 07 '11 at 19:46
  • I'd love to use PDF files instead. Or convert the doc files to html on my end. But the guy that signs my paycheck says that they are to be word docs... so they have to be word docs. – Brian S Jul 07 '11 at 20:03

4 Answers4

2

Have a look at window.navigator.onLine

mplungjan
  • 169,008
  • 28
  • 173
  • 236
1

You could try an ajax request to a reliable site.

// THIS IS OUR PAGE LOADER EVENT!
window.onload = function() { 
    testConnection();
};

function testConnection(){
    var xmlhttp;

    if (window.XMLHttpRequest){
        xmlhttp=new XMLHttpRequest();
    } else {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function(){
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
            alert(xmlhttp.responseText);
        } else if (xmlhttp.readyState==4){
            alert("NO INTERNET DETECTED!");
        }
    }

    xmlhttp.open("GET","http://www.w3schools.com/ajax/ajax_info.txt",true);
    xmlhttp.send();
}

Now you will have to play around with it for a way to use the response because it is asynchronous, but this will work to test a connection.

Utilitron
  • 402
  • 3
  • 10
  • AJAX is working with external domains? – The Mask Jul 07 '11 at 20:00
  • Internet explorer will if it is a local html file. I did not test in firefox or chrome. I am not exactly sure why it works, but you can test it yourself. But again it has to be a local file opened with internet explorer. – Utilitron Jul 07 '11 at 20:18
  • It is explained a bit here: http://msdn.microsoft.com/en-us/library/ms537505%28v=vs.85%29.aspx there is a table under the Cross-Domain and Zone Policy section showing what is allowed. – Utilitron Jul 07 '11 at 20:23
  • in older versions of internet explorer is normal this works. but after IE 6.0 version, call external url with ajax are blocked for reasons security. but oddly this works fine in Firefox Mozilla/5.0, I dont know,is new feature? – The Mask Jul 07 '11 at 20:25
0

Previously discussed here: Check if Internet Connection Exists with Javascript? and here: How to know if the network is (dis)connected?.

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
0
GM_xmlhttpRequest({
  method: "GET",
  url: "http://www.google.com/",
  onload: function(response) {
    if(response.responseText != null) { 
       alert("OK");
    } else { 
      alert("not connected");
    }
  }
});
  • second alternative

    var HaveConnection = navigator.onLine;

The Mask
  • 17,007
  • 37
  • 111
  • 185