SyntaxStudy
Sign Up
jQuery Intermediate 3 min read

Offline Detection

Offline Detection

Detect online/offline status with the navigator.onLine property and online/offline events to show appropriate UI.

Example
$(window).on("online offline", function() {
  const online = navigator.onLine;
  if (online) {
    $("#offline-banner").hide();
    // Re-sync queued actions
    processOfflineQueue();
  } else {
    $("#offline-banner").show().text("You are offline. Changes will sync when reconnected.");
  }
});
// Check on load
if (!navigator.onLine) $("#offline-banner").show();
// Queue actions for later sync
const offlineQueue = [];
function queueAction(action) {
  if (navigator.onLine) action();
  else offlineQueue.push(action);
}
Pro Tip

navigator.onLine can be true even with a slow connection — verify with an actual HTTP request for critical checks.