-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.js
53 lines (42 loc) · 1.85 KB
/
connection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Function to create and show the connection status message
function createConnectionStatus() {
const connectionContainer = document.querySelector('.connection-container');
// Create the status box element
const statusBox = document.createElement('div');
statusBox.className = 'status-box';
statusBox.style.display = 'none'; // Initially hide the status box
// Create the main message element
const mainMessage = document.createElement('p');
mainMessage.className = 'main-message';
mainMessage.textContent = 'You lost internet connection';
// Create the sub-message element
const subMessage = document.createElement('p');
subMessage.className = 'sub-message';
subMessage.textContent = 'We are trying to reconnect you';
// Append main and sub messages to the status box
statusBox.appendChild(mainMessage);
statusBox.appendChild(subMessage);
// Append the status box to the connection container
connectionContainer.appendChild(statusBox);
return statusBox; // Return the created status box for future reference
}
// Event listener for online and offline events
window.addEventListener('online', () => {
showConnectionStatus(true);
});
window.addEventListener('offline', () => {
showConnectionStatus(false);
});
// Function to show or hide the status box based on the online status
function showConnectionStatus(online) {
const statusBox = createConnectionStatus(); // Create the status box if it doesn't exist
if (online) {
// User is online, hide the status message
statusBox.style.display = 'none';
} else {
// User is offline, show the status message
statusBox.style.display = 'block';
}
}
// Initial check and display based on the user's online status
showConnectionStatus(navigator.onLine);