I'm trying to verify the user's internet connection before form submit, so that my JavaScript validation function can throw an alert and return false if no connection.
I've tried: navigator.onLine - doesn't work in FF (only looks at whether browser in 'offline mode')
AJAX request to my own site:
$.ajax({
type: "GET",
url: "internet_test.php",
async: false, //trying to force a response before continuing
timeout:8000,
success: function(ret_text) {
if(ret_text == 'yes') {
return true;
} else {
alert("No Connection 1");
return false;
}
},
error: function(x, t, m) {
if(t==="timeout") {
alert("No Connection 2");
return false;
} else {
alert("No Connection 3"); //this is the one triggered
return false;
}
}
});
I get the alert for the third type of error, but then the form submits anyway. I'm guessing that the error overrides the fact that I tried to specify a synchronous check, and the JavaScript validator finishes before the AJAX can return false.
Is there a good way around this? Better way to do it?
Thanks!