detect change on navigator.online

Jack picture Jack · Aug 11, 2011 · Viewed 9.5k times · Source

How can I detect if the navigator changed your state to online/offline?

something like:

var oldState = navigator.onLine; 
window.navigator.onlinechange = function(evnt,newState) { 
         alert('your changed from' + oldState + ' to' + newState + 'state');
}

Answer

Henrik Albrechtsson picture Henrik Albrechtsson · Apr 14, 2016

With the help of classes on body and this code you can find

window.ononline = function() {
    alert('You are now online');
}

window.onoffline = function() {
    alert('You are now offline');
}