Skip to content

Instantly share code, notes, and snippets.

@inezabonte
Created October 28, 2020 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inezabonte/d24d66f0e3a406ec2eaa3aef61f14601 to your computer and use it in GitHub Desktop.
Save inezabonte/d24d66f0e3a406ec2eaa3aef61f14601 to your computer and use it in GitHub Desktop.
Checking if you are connected to the internet
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"
})
<!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>
.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