How to alert users when connection is gone using Offline.js
Asked Answered
P

2

5

I'm trying to alert users when they are offline, and show a simple message. I'm using Offline.js as a library (Offline.js website).

I want to write JS and not use Eager to install it automatically.

Here is what I wrote, but it doesn't seem to be working. Can someone help me with more details?

I couldn't find a sample with code on the library website.

Offline.options = {checkOnLoad: true, checks: {image: {url: 'http://www.vitaminedz.com/photos/49/02-49565-front-de-mer-a-oran.jpg'}, active: 'image'}}

Offline.check();
Offline.on('up', alert('up'));
Offline.on('down',alert('down'));

JSFiddle Code + DEMO

Polar answered 17/4, 2015 at 12:29 Comment(1)
Note that github doesn't let you link to files from jsfiddle like that - it serves them with a text/plain mime type, so your fiddle is (currently) non-functional.Taddeusz
F
7

Your handlers are not set properly. It should be like this:

alert(Offline.check());
Offline.on('up', function() {
    alert('up');
});
Offline.on('down', function() {
    alert('down')
});
Frederigo answered 17/4, 2015 at 13:2 Comment(1)
Offline.js not only trigger these messages but re-initiate the ajax that was broke during the connection broke. But after success methods of such Ajax are not triggered. How to trigger them?Rafaelrafaela
N
0

After Linking Your js and css theme of the offline you then add these code.

<script type="text/">
var run = function(){
if (Offline.state === 'up')
Offline.check();
}
 setInterval(run, 1000);
</script>

NOTE: It Will Work only if you call your jquery.js code above it.

Neck answered 5/8, 2016 at 5:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.