var checkAfter=15; //15 seconds
setTimeOut(CheckNavigatorState,checkAfter*1000);
function CheckNavigatorState(){
if(navigator.onLine){
// --- Add javascript src OR call js functions here
}else{
//----remove javascript source OR stop functions
setTimeOut(CheckNavigatorState,checkAfter*1000);
}
}
I would like to mention that it is not easy to tell whether the browser is offline or not.
Some browser vendors say the browser is offline when computer loose its connection to the network, which is not really exact. We know that you can have access to the LAN but not to the internet. So to do what you may need to ping a real distant server, such as Google
.
EDITS: To ping with JavaScript, check this fiddle (edited)