From 646bce722728467fc03ee03add97fe5c186ae179 Mon Sep 17 00:00:00 2001 From: Robson Lages Date: Fri, 18 Jan 2019 07:19:55 -0200 Subject: [PATCH] Update offline-events.js I changed 2 points: 1 - If the connection returns for example status 404, it should indicate how offline, before it returned online. 2 -When the connection already starts offline, it used to be online because of line 39 (// lastOnLineStatus = false) --- offline-events.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/offline-events.js b/offline-events.js index fa1db11..7dacd56 100644 --- a/offline-events.js +++ b/offline-events.js @@ -15,9 +15,14 @@ function testConnection() { xhr.open('HEAD', '/', false); // async=false try { xhr.send(); - onLine = true; + if(xhr.status < 299){ + onLine = true; + }else{ + onLine = false; + } + } catch (e) { - // throws NETWORK_ERR when disconnected + // throws NETWORK_ERR when disconnected onLine = false; } @@ -25,14 +30,13 @@ function testConnection() { } var onLine = true, - lastOnLineStatus = true; +lastOnLineStatus = true; // note: this doesn't allow us to define a getter in Safari navigator.__defineGetter__("onLine", testConnection); testConnection(); - if (onLine === false) { - lastOnLineStatus = false; + //lastOnLineStatus = false; // trigger offline event triggerEvent('offline'); }