简体   繁体   中英

How to callback online/offline status?

Once I was fiddling on jsfiddle, I lost my internet connection. Then, the site alert me that I have no internet connection. Here, give a try by clicking an disabling your connection :

a sample code

They somehow implement a code which checking internet connection without any ajax request unlike this .

I found this snippet after some digging

Heyoffline.prototype.setup = function() {
  this.events = {
    element: ['keyup', 'change'],
    network: ['online', 'offline'] // THIS PART 
  };
  this.elements = {
    fields: document.querySelectorAll(this.options.elements.join(',')),
    overlay: document.createElement('div'),
    modal: document.createElement('div'),
    heading: document.createElement('h2'),
    content: document.createElement('p'),
    button: document.createElement('a')
  };

yet I don't understand how it works since it's generated automatically. LINK

Can anyone shed some light, so I can implement something like this in my system.

The magic is done here

Heyoffline.prototype.networkEvents = function(event) {
    return addEvent(window, event, this[event]);
};

Which is called in attachEvents

    Heyoffline.prototype.attachEvents = function() {
        var event, field, _i, _j, _len, _len1, _ref, _ref1,
        _this = this;
        _ref = this.elements.fields;
        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
            field = _ref[_i];
            this.elementEvents(field);
        }
        _ref1 = this.events.network;
        for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
            event = _ref1[_j];
            this.networkEvents(event); //Called here
        }
        return addEvent(window, 'resize', function() {
            return _this.resizeOverlay();
        });
    };

The event, either online or offline is passed to this, the addEvent function

addEvent is defined as:

addEvent = function(element, event, fn, useCapture) {
    if (useCapture == null) {
        useCapture = false;
    }
    return element.addEventListener(event, fn, useCapture);
}; 

So really going by what I can see to use the online/offline function all you need to do is call:

//online
window.addEventListener('online', function(){alert('You are online')}, false);
//offline
window.addEventListener('offline', function(){alert('You are online')}, false);

and bobs ya uncle

Tested here as well: http://jsfiddle.net/W4EKh/

The alert comes up right after the jsfiddle one does so I am pretty certain thats how it works...

also, heres something to help out :)...

function networkListener(a, b){
    var offlineF, onlineF;
    if(a instanceof Function && b instanceof Function){
        onlineF = a;
        offlineF = b;
    }else if(a instanceof Function){
        if(typeof b === "string"){
            if(b === "online"){
                onlineF = a;
            }else if(b === "offline"){
                offlineF = a;
            }
        }else{
            onlineF = a;
        }
    }
    if(typeof onlineF !== "undefined"){
        window.addEventListener('online', onlineF, false);
    }
    if(typeof offlineF !== "undefined"){
        window.addEventListener('offline', offlineF, false);
    }
}

And here you can try it out...

http://jsfiddle.net/47N6L/2/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM