Online/Offline Event Detection
The navigator.onLine
attribute returns:
Since many cases return true
, you should treat with care situations of
getting false positives, as we cannot always assume that true
value means
that Electron can access the Internet. For example, in cases when the computer
is running a virtualization software that has virtual Ethernet adapters in “always
connected” state. Therefore, if you want to determine the Internet access
status of Electron, you should develop additional means for this check.
Example
Starting with an HTML file , this example will demonstrate how the navigator.onLine
API can be used to build a connection status indicator.
```html title=”index.html” <!DOCTYPE html>
Connection status:
Finally, create a main.js
file for main process that creates the window.
```js title=’main.js’ const { app, BrowserWindow } = require(‘electron’)
const createWindow = () => { const onlineStatusWindow = new BrowserWindow({ width: 400, height: 100 })
app.whenReady().then(() => { createWindow()
app.on(‘activate’, () => { if (BrowserWindow.getAllWindows().length === 0) { createWindow() } }) })
app.on(‘window-all-closed’, () => { if (process.platform !== ‘darwin’) { app.quit() } }) ```
After launching the Electron application, you should see the notification: