How do you check if there is an internet connection using Javascript? That way I could have some conditionals saying "use the google cached version of JQuery during production, use either that or a local version during development, depending on the internet connection".
You can try by sending XHR Requests a few times, and then if you get errors it means there's a problem with the internet connection.
Edit: I found this JQuery script which is doing what you are asking for, I didn't test it though.
edit: this solution is only for subsequent calls when the page is already loaded and we need to detect if we still have internet access. that's how i interpreted the first version of the question. navigator.online is more elegant for the 'first-time-check' though.
google should have a good uptime for this test :-)
the first function is an event handler which is only being called, when the get-function will not succeed. the callback of the get-function will only be called on success.
$('body').ajaxError(function() {
alert("failed");
});
$.get('http://www.google.de', function(data) {
alert("success");
});
Sending XHR requests is bad because it could fail if that particular server is down. Instead, use googles API library to load their cached version(s) of jQuery.
You can use googles API to perform a callback after loading jQuery, and this will check if jQuery was loaded successfully. Something like the code below should work:
<script type="text/javascript">
google.load("jquery");
// Call this function when the page has been loaded
function test_connection() {
if($){
//jQuery WAS loaded.
} else {
//jQuery failed to load. Grab the local copy.
}
}
google.setOnLoadCallback(test_connection);
</script>
The google API documentation can be found here.
You could try:
var online = navigator.onLine;
Read more about the W3C's spec on offline web apps, however be aware that this will work best in modern web browsers, doing so with older web browser's may be pretty tough.
Alternatively an XHR request to your own server isn't that bad method for testing your connectivity. Consider one of the other answers state that there are too many points of failure for an XHR, but if your XHR is flawed when establishing it's connection than it'd also be flawed during routine use anyhow.
Therefore it's a suitable fallback from navigator.onLine
.
It would be a really bad idea to make an XHR request to someone else's server, even google.com for that matter. Make the request to your server, or not at all.
The best option in your case might be:
In your html <head>
tag
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script id="jquery_loader"></script>
In your js file (or possibly still in the <head>
if you want):
function loadJQuery() {
if (navigator.onLine(connected)) {
google.load("jquery", "1.4.1");
} else {
document.getElementById('jquery_loader').src = '/javascripts/jquery.js';
}
}
Then right before your close </body>
tag:
<script>
loadJQuery();
</script>
Bizarrely enough, the reason this works is because writing to the objects attributes calls the browsers parsing engine again, which will have to parse the script tag and thus include the local file if the browser is deemed offline by the prior method.
You might say, why put the script tag at the end of the <body>
? This is because without jQuery's ready()
function, we are stuck to emulate the readiness of the DOM, and this would be the best way to do that, the only native option would be the onload
attribute of your body tag, but this happens far later in load sequence of your page (just after the reflow and draw).
I basically just explained how browsers work for no reason.
[sighs]