1

I am trying to understand how "Offline.js" works. I have the following script which I have working under the following circumstances:

Device connected to the internet via WiFi. If I disable the WiFi, the offline events are triggered. When I enable the WiFi the web page returns to running as normal.

The issue I have, if I display/turn off the Wifi access point rather than the device's WiFi, after three or more attempts to connect to the internet offline fails and the browser "Chrome" displays the default message "Not connected to the Internet".

Should Offline.js work under those circumstances, i.e WiFi access point turned off.

The script I am using is:

var active;
var pageno = "<?php echo $pageNo;?>";
var totalPages_fids = "<?php echo $totalPages_fids;?>";
var RefreshRate = "<?php echo $ArriveScreenRefreshRate;?>";
var pageStrNo = "<?php echo $pageStrNo;?>";
var run = function(){
if (Offline.state === 'up') {
//Offline.check();
Offline.options = {
    checkOnLoad: true, 
    requests: true,
    checks:{
        image: {
            url: 'tiny-image.gif'}, 
            active: 'image'}
    }
active = 1;
console.log(Offline);
console.log(active);
if (active == 1) { // IF VAR "ACTIVE" == 1 EXECUTE THE CODE BELOW
    if(pageno == totalPages_fids) {
        window.setTimeout(function(){
            window.location.href = "depart_ad24.php";
        }, RefreshRate);

    } else { 

    function IsBytec()
    {
    try
    {
    if(window.external.getBytProperty('SVNRev'))
    return true;
    }
    catch(err){}
    return false;
    }

    function ScrRefresh() {
        var URL = 'depart24.php?pageNum_fids='+pageStrNo+'&totalPages_fids='+totalPages_fids+'';

        if (IsBytec()) {
            window.external.Browser(1).parentWindow.location.href = URL;
        } else {
            window.location = URL;
        }
        return;
    }
    window.setInterval(ScrRefresh, RefreshRate) 
}
}
}
}

setInterval(run, RefreshRate);
active = 0;
console.log(active);

When I run the page and monitor the process in Chrome console this is what I see. Console: enter image description here

And then the browser displays:

enter image description here

Many thanks in advance for your comments and time.

G07cha
  • 4,009
  • 2
  • 22
  • 39
DCJones
  • 3,121
  • 5
  • 31
  • 53
  • Could you specify, are you asking if it should show "Not connected" earlier? – FINDarkside May 07 '18 at 14:49
  • @FINDarkside Hi. What I am asking is why the script works if I disable the devices WiFi but does not work if I disable the WiFi access point or the internet router. Wth the AP or the router disabled "Chome" just displays it's default page "Cannot connect to the Internet". I was expecting the Offline.js script to work as it does when disabling the devices WiFi. – DCJones May 07 '18 at 14:53
  • Ok if you're refreshing the page you need to check if there's connection before you do. I'm still not sure what you mean by "after three or more attempts to connect to the internet offline fails". You could call Offline.check(), and then on confirmedUp event refresh the page. Keep in mind that confirmedUp is also called on page load when checkOnLoad: true – FINDarkside May 07 '18 at 15:11
  • @FINDarkside Hi again, I have changed the code to capture the status of "up" or "down". If "up" the page refresh gets executed. See edited question. As I pointed out in my question, if I disable the device WiFi and monitor what is happening in the Chrome developers console, see edit question for images. – DCJones May 07 '18 at 19:11
  • Since it js it checks the device connection ability state and not if you actually connect to internet. – A. Meshu May 07 '18 at 19:42
  • @A. Meshu Thank for your reply and it does anwser a lot of questions. – DCJones May 07 '18 at 19:47
  • 1
    Answer by @A.Meshu is partly false. offline.js does check your connection status overall, but when you disable your wifi it will instantly know that there's no connection since an event is emitted. When you disable your WiFi access point no such event is emitted since the only way to test if there's internet connection is to request something. That's what offline.js does, at least once it detects you're offline it will try to request something every few minutes. You can do it manually with Offline.check() – FINDarkside May 07 '18 at 19:57
  • @FINDarkside i wanted to answer with your idea for manual check as a solution. Since you comment so well do it yourself - i'm still not home (-: – A. Meshu May 07 '18 at 21:06

0 Answers0