Phonegap online and offline detection


So in cleaning up some of the functionality I wanted to detect when a user is online or offline to give them a bit of a hint before performing a search. Luckily phonegap makes this very straightforward as it has two events you can listen out for; ‘online’ and ‘offline’. So effectively all you need to do is bind a couple of functions to these events once phonegap is loaded.

Because I’m using knockout js I just used a single observable in my viewmodel called ‘online’. The respective events set this value to true or false. In turn the view changed the display message to suit the state of this property. It’s easy to test on your phone as you just need to toggle airplane mode on or off.