Questions tagged [offline.js]

Offline.js is a library to automatically alert your users when they've lost internet connectivity, like Gmail. It captures AJAX requests which were made while the connection was down, and remakes them when it's back up, so your app reacts perfectly. It has a number of beautiful themes and requires no configuration.

Improve the experience of your app when your users lose connection.

  • Monitors ajax requests looking for failure
  • Confirms the connection status by requesting an image or fake resource
  • Automatically grabs ajax requests made while the connection is down and remakes them after the connection is restored.
  • Simple UI with beautiful themes
  • 3kb minified and compressed

Install with Eager

The easiest way to add Offline to your site is with Eager. Click Install to see a live preview of Offline on your website.

Manual Installation

Include the javascript, one of the themes, and one of the languages on your site. You're done!

To use only the JavaScript API without a UI indicator, simply leave out the CSS file.

If you'd like to get a peek at how it looks on your site, disconnect your internet, or try out the simulator.

Advanced

Optionally, you can provide some configuration by setting Offline.options after bringing in the script.

Options (any can be provided as a function), with their defaults:

```javascript { // Should we check the connection status immediatly on page load. checkOnLoad: false,

// Should we monitor AJAX requests to help decide if we have a connection. interceptRequests: true,

// Should we automatically retest periodically when the connection is down (set to false to disable). reconnect: { // How many seconds should we wait before rechecking. initialDelay: 3,

// How long should we wait between retries.
delay: (1.5 * last delay, capped at 1 hour)

},

// Should we store and attempt to remake requests which fail while the connection is down. requests: true,

// Should we show a snake game while the connection is down to keep the user entertained? // It's not included in the normal build, you should bring in js/snake.js in addition to // offline.min.js. game: false } ```

Properties

Offline.check(): Check the current status of the connection.

Offline.state: The current state of the connection 'up' or 'down'

Offline.on(event, handler, context): Bind an event. Events:

  • up: The connection has gone from down to up
  • down: The connection has gone from up to down
  • confirmed-up: A connection test has succeeded, fired even if the connection was already up
  • confirmed-down: A connection test has failed, fired even if the connection was already down
  • checking: We are testing the connection
  • reconnect:started: We are beginning the reconnect process
  • reconnect:stopped: We are done attempting to reconnect
  • reconnect:tick: Fired every second during a reconnect attempt, when a check is not happening
  • reconnect:connecting: We are reconnecting now
  • reconnect:failure: A reconnect check attempt failed
  • requests:flush: Any pending requests have been remade
  • requests:hold: A new request is being held

Offline.off(event, handler): Unbind an event

Checking

By default, Offline makes an XHR request to load your /favicon.ico to check the connection. If you don't have such a file, it will 404 in the console, but otherwise work fine (even a 404 means the connection is up). You can change the URL it hits (an endpoint which will respond with a quick 204 is perfect):

javascript Offline.options = {checks: {xhr: {url: '/connection-test'}}};

Make sure that the URL you check has the same origin as your page (the connection method, domain and port all must be the same), or you will run into CORS issues. You can add Access-Control headers to the endpoint to fix it on modern browsers, but it will still cause issues on IE9 and below.

If you do want to run tests on a different domain, try the image method. It loads an image, which are allowed to cross domains.

javascript Offline.options = {checks: {image: {url: 'my-image.gif'}, active: 'image'}}

The one cavet is that with the image method, we can't distinguish a 404 from a genuine connection issue, so any error at all will appear to Offline as a connection issue.

Offline also includes a check called 'up' and another called 'down' which will always report being up or down respectively for testing. You can activate them by setting the active option, adding a data attribute to your script tag with the name data-simulate and value 'up' or 'down', or by setting localStorage.OFFLINE_SIMULATE to 'up' or 'down'.

Reconnect

The reconnect module automatically retests the connection periodically when it is down. A successful AJAX request will also trigger a silent recheck (if interceptRequests is not false).

You can disable the reconnect module by setting the reconnect to false. Reconnect can be configured by setting options on the reconnect setting.

Requests

The requests module holds any failed AJAX requests and, after deduping them, remakes them when the connection is restored.

You can disable it by setting the requests setting to false.

You can also set deDupBody to be true if you want deduping to also take into account the content of the request.

Dependencies

None!

Browser Support

Modern Chrome, Firefox, Safari and IE8+

Documentation

16 questions
3
votes
1 answer

How can I make Offline.js distinguish between genuine offline behavior and the server being down?

Our team has started using Offline.js, a library that can detect if a user is offline. We have currently set it up to look for an image on our server using the following code: Offline.options = {checks: {xhr: {url:…
Thunderforge
  • 19,637
  • 18
  • 83
  • 130
2
votes
0 answers

Suppress Message Box "Turn Off Airplane Mode or Use Wi-Fi to Access Data" in Safari for online/offline webapp

I have a webapp without a manifest yet that should work in both online and offline mode. I am using Offline.js to detect the network state. When first going offline a message box "Turn Off Airplane Mode or Use Wi-Fi to Access Data" pops up and ends…
Stefan
  • 137
  • 13
2
votes
3 answers

Offline.js - checking while online?

Here is my code below. Simply, I don't know how to make it that if the connection transitions from ONLINE to OFFLINE, that the event which updates the css (https://github.com/hubspot/offline) will be triggered. If it goes from OFFLINE to ONLINE,…
b85411
  • 9,420
  • 15
  • 65
  • 119
1
vote
0 answers

Understanding offline.js for monitoring internet connection

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…
DCJones
  • 3,121
  • 5
  • 31
  • 53
1
vote
1 answer

Offline.js doesnt work

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, …
1
vote
2 answers

Making offline.js work with turbolinks

I am using offline.js with turbolinks and on initial page load it works fine. But if I click another links then this wont work until I refresh the page. I can see the Offline.state is down but still the view is not showing. Is there any way to…
Abhilash
  • 2,864
  • 3
  • 33
  • 67
1
vote
1 answer

Hiding Submit Button with offline.js

I am using offline.js (0.7.18) and its working fine, when I go on/offline the indicator changes state, all good so far. I can also see on Offline JS Simulate UI, that a login panel is having its style toggled between display:block and display:none…
1
vote
1 answer

Offline.js - Multi-language

I am use Offline.js. Offline.js is a library to automatically alert your users when they've lost internet connectivity. The Offline.js provide the different language in the css file but each one language contains his css file. In my website I detect…
saeta
  • 4,048
  • 2
  • 31
  • 48
1
vote
1 answer

Offline.js error message

I'm using Offline.js library to notify users when they lost internet connectivity. Currently this library intercepts every AJAX request and decides if user have a connection. Here's a function which is automatically called when connectivity is…
0
votes
0 answers

Is there maintened plugin doing same thing than 'offline.js'?

I'm looking for a way to check real time network connection in a webApp. "Offline.js" do exaclty what I want but there is to much possibilites than only check connection and is not maintened. Do you know another plugin/code doing exactly the same…
yanisla35
  • 143
  • 1
  • 8
0
votes
1 answer

Offline js not detecting down internet

I am using offline js for internet detecting up and down. My code is below $(document).ready(function () { Offline.options = { reconnect: { initialDelay: 10, delay: (5) } } …
Faisal
  • 81
  • 4
0
votes
0 answers

XHR error not caught

I am having an issue where an XHR error is not properly caught. I am using the Offline.js library to do some checking. The code the library is performing is: Offline.checks.xhr = function() { var e, xhr; xhr = new XMLHttpRequest; xhr.offline =…
Hendrik
  • 4,849
  • 7
  • 46
  • 51
0
votes
1 answer

Working with OfflineJS and AngularJS

I am working on an angular app where I want to integrate OfflineJS functionality. I have created a general service for getting and posting data to/from an API,and a specific service for each module. Here is the code app.service('MethodProvider',…
Ghazanfar Khan
  • 3,648
  • 8
  • 44
  • 89
0
votes
0 answers

Offline.js Cross-domain Image gives undefined

I want my web app to know if it's online (to avoid my Google maps functions), so I use Offline.js. I'm trying to access an image from our website, but the web app is not hosted online (it's run locally), so the access is cross-domain. Here is my…
xinthose
  • 3,213
  • 3
  • 40
  • 59
0
votes
1 answer

Offline.js Issue

I am use Offline.js. Offline.js is a library to automatically alert your users when they've lost internet connectivity. I have used the offline.js version 0.5.0.0, I have set the offline.min.js in _Layout Page. It's worked perfectly in google chrome…
user3505487
  • 95
  • 2
  • 6
1
2