1

I am using Offline.js to check my Internet connection in a simple web app. The problem is that i receive only the first connection message and then nothing.

$(document).ready(function () {
Offline.options = {
    checkOnLoad: true,
    interceptRequests: true,
    reconnect: {
        initialDelay: 3
    },
    requests: true,
    game: false
};

console.log(Offline.check());

Offline.on("up",function () {
    console.log("connected");
});
Offline.on("down", function () {
    console.log("disconnected");
});
});

1 Answers1

1

Your script works fine, unless you run it locally using the file:// scheme.

The point is that Offline.js makes requests to /favicon.ico to the domain, it is currently running on. For the local scheme, requests to itself can yield a security threat. Therefore it is disabled by default.

  1. You can start Google Chrome using the --allow-file-access-from-files argument to make it work.
  2. In Mac or Linux, go into the folder of your files and run python3 -m http.server. Then go to localhost:8000/ in your browser and locate your HTML file.

Using these approaches, the HTML files will be served over HTTP and it can make proper AJAX calls and Offline.js works again.

In short: Offline.js does not work in file:// schemes.

meisterluk
  • 804
  • 10
  • 19
  • Thanks for the tips, the problem is that with this approach, other users need to use google Chrome with "--allow-file-access-from-files". Is there any way to make it works automatically? – Gianmarco6000 Mar 15 '17 at 12:19
  • Sadly no. You have to provide a server (locally or in the WWW) or start Google Chrome with the argument. For example Mozilla Firefox does not even provide such an option. – meisterluk Mar 15 '17 at 12:33