Check if there is a Jquery internet connection

4

Does anyone know of a good function or a good way to check for an internet connection before making an ajax request?

    
asked by anonymous 21.07.2016 / 14:03

1 answer

6

It's possible, try:

var online = navigator.onLine; // true ou false, (há, não há conexão à internet)
if(online) {
    // efetuar pedido ajax
}

To test, create a file on your computer, eg: online.html and type:

<script>
    var online = navigator.onLine;
    alert(online);
</script>

Open it in the browser and disconnect / connect the connection to test

    
21.07.2016 / 14:08