Created
October 28, 2020 12:48
-
-
Save inezabonte/d24d66f0e3a406ec2eaa3aef61f14601 to your computer and use it in GitHub Desktop.
Checking if you are connected to the internet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let connection = document.querySelector('.connection') | |
let status = document.querySelector('.status') | |
window.addEventListener("load", (event) => { | |
let result = navigator.onLine; | |
if(result){ | |
status.textContent = "Connected" | |
} else{ | |
connection.classList.toggle('offline') | |
status.textContent = "Disconnected" | |
} | |
}); | |
window.addEventListener('online', (event) => { | |
connection.classList.toggle('offline') | |
status.textContent = "Connected" | |
}) | |
window.addEventListener('offline', (event) => { | |
connection.classList.toggle('offline') | |
status.textContent = "Disconnected" | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<link rel="stylesheet" href="style.css"> | |
<title>Online Status</title> | |
</head> | |
<body> | |
<div class="connection"> | |
<span class="status"></span> | |
</div> | |
<script src="app.js"></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.connection{ | |
width: 180px; | |
height: 50px; | |
border-radius: 50px; | |
display: flex; | |
justify-content: center; | |
align-items: center; | |
font-size: 18px; | |
font-family: Arial, Helvetica, sans-serif; | |
font-weight: bold; | |
background-color: rgba(2, 255, 2, 0.33); | |
color: #49b927; | |
} | |
.offline{ | |
background-color: rgba(255, 0, 0, 0.1); | |
color: red; | |
transition: all 0.3s ease-out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment