Devnote

Web Development Blog Company, Services India

MySQL Error 121 Duplicate key on write - Practical Repair Strategy
MySQL Error 121 Duplicate key on write – Practical Repair Strategy
February 13, 2026
PHP cURL error 60: SSL certificate problem - Windows & Linux Fix
PHP cURL error 60: SSL certificate problem – Windows & Linux Fix
February 11, 2026
Fix localhost refused to connect After Installing XAMPP / WAMP
Fix “localhost refused to connect” After Installing XAMPP / WAMP
February 9, 2026
Composer Error Memory exhausted When Installing Packages - Real Fix
Composer Error: Memory exhausted When Installing Packages – Real Fix
February 6, 2026
MySQL 8 Error Unknown collation: utf8mb4_0900_ai_ci - Safe Migration Guide
MySQL 8 Error Unknown collation: utf8mb4_0900_ai_ci – Safe Migration Guide
February 4, 2026
Devnote

Type and hit Enter to search

  • Home
  • Laravel
    • Auth
    • Migration
    • DATATABLE
    • Yajra
  • wordpress
    • plugin
    • WPBakery
    • woocommerce
  • PHP
    • MYSQL
    • Ajax
  • Blog
  • Informational
    • About us
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
  • Contact US
Devnote
  • Home
  • Laravel
    • Auth
    • Migration
    • DATATABLE
    • Yajra
  • wordpress
    • plugin
    • WPBakery
    • woocommerce
  • PHP
    • MYSQL
    • Ajax
  • Blog
  • Informational
    • About us
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
  • Contact US
Detect Internet Connection using HTML CSS & JavaScript

Detect Internet Connection using HTML CSS & JavaScript

Image
Devnote team
June 11, 2021
4
1.4K Views
0 Comments

In this tutorial, we will explore how to detect an internet connection using HTML, CSS, and JavaScript. Modern web applications rely heavily on connectivity. When the user goes offline or their internet becomes unstable, it can affect page performance, API calls, data saving, and overall user experience. That’s why showing a clear connection status notification has become essential in web development.

We will build a simple system that displays a toast notification whenever the user loses or regains internet access. When the connection is active, the message will indicate that everything is working fine. When the connection drops, the user will immediately see a visual warning. This is a practical approach used in many modern apps like Gmail, YouTube, and social media platforms. They detect online and offline events and communicate them instantly.

Our example uses a clean HTML layout, basic CSS styling, and JavaScript’s built-in online and offline events. These events help us listen to changes in the browser’s network status without refreshing the page. With a few lines of JavaScript, we can check connection state, show custom notifications, and improve user interaction. By the end of this tutorial, you will understand how to detect internet connectivity changes in real time and display user-friendly alerts using simple front-end code.

Check Internet Connection

Simple To create this program [Checking Online-Offline Status]. we will need to create three files, HTML File, CSS File, and JavaScript File. Simply, create these files and just paste the following codes into your files.

First, create an HTML file with the name index.html and paste the below codes into your HTML file.

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>How to check Detect Internet Connection using HTML CSS & JavaScript - devnote.in</title>
    <link rel="stylesheet" href="style.css">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
    <div class="main_wrapper">
        <div class="toast_cls">
            <div class="content">
                <div class="icon"><i class="fa fa-wifi" aria-hidden="true"></i></div>
                <div class="details">
                    <span>You're online</span>
                    <p>Internet is connected.</p>
                </div>
            </div>
            <div class="close-icon"><i class="fa fa-times"></i></div>
        </div>
    </div>
  <script src="script.js"></script>
</body>
</html>

script.js

const wrapper = document.querySelector(".main_wrapper"),
toast = wrapper.querySelector(".toast_cls"),
title = toast.querySelector("span"),
subTitle = toast.querySelector("p"),
wifiIcon = toast.querySelector(".icon"),
closeIcon = toast.querySelector(".close-icon");

window.onload = ()=>{

    // First example start 
    // let xhr = new XMLHttpRequest();
    // xhr.open("GET", "https://reqres.in/api/users", true);
    // xhr.onload = ()=>{
    //     if(xhr.status == 200 && xhr.status < 300){
    //         toast.classList.remove("offline");
    //         title.innerText = "You're online";
    //         subTitle.innerText = "Internet is connected.";
    //         wifiIcon.innerHTML = '<i class="fa fa-wifi"></i>';
    //         closeIcon.onclick = ()=>{
    //             wrapper.classList.add("hide");
    //         }
    //         setTimeout(()=>{
    //             wrapper.classList.add("hide");
    //         }, 5000);
    //     }else{
    //         offline();
    //     }
    // }
    // xhr.onerror = ()=>{
    //     offline();
    // }
    // xhr.send();
    // First example end 
    
    // Second example start 
    function ajax(){
        $.ajax({
            url: "https://reqres.in/api/users",
            type: "post",
            success: function (response, textStatus, xhr) {
                if(xhr.status == 200 || xhr.status < 300) {
                    toast.classList.remove("offline");
                    title.innerText = "You're online";
                    subTitle.innerText = "Internet is connected.";
                    wifiIcon.innerHTML = '<i class="fa fa-wifi"></i>';
                    closeIcon.onclick = ()=>{
                        wrapper.classList.add("hide");
                    }
                    setTimeout(()=>{
                        wrapper.classList.add("hide");
                    }, 5000);
                } else {
                    offline();
                }
            },
            error: function(jqXHR, textStatus, errorThrown) {
                offline();
            }
        });
    }
    // Second example end 
    
    function offline() {
        wrapper.classList.remove("hide");
        toast.classList.add("offline");
        title.innerText = "You're offline";
        subTitle.innerText = "Sorry! Internet is disconnected.";
        wifiIcon.innerHTML = '<i class="fa fa-times"></i>';
    }

    // this setInterval function call ajax after every 20000ms
    setInterval(()=>{
        ajax();
    }, 2000);
}

style.css

@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500;600;700&display=swap');
*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Poppins', sans-serif;
}
body{
    overflow: hidden;
    background: #f2f2f2;
}
.main_wrapper{
    position: absolute;
    right: 40px;
    bottom: 20px;
    animation: toast_animation 2s ease forwards;
}
@keyframes toast_animation {
    0% {
        transform: translateX(100%);
    }
    40% {
        transform: translateX(-10%);
    }
    80%, 100%{
        transform: translateX(20px);
    }
}

.main_wrapper.hide {
    animation: hide_toast_animation 1s ease forwards;
}

@keyframes hide_toast_animation {
    0% {
        transform: translateX(20px);
    }
    40% {
        transform: translateX(-10%);
    }
    80%, 100% {
        opacity: 0;
        pointer-events: none;
        transform: translateX(100%);
    }
}
.main_wrapper .toast_cls{
    background: #fff;
    padding: 20px 15px 20px 20px;
    border-radius: 10px;
    border-left: 5px solid #2ecc71;
    box-shadow: 1px 7px 14px -5px rgba(0,0,0,0.15);
    width: 430px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.main_wrapper .toast_cls.offline{
    border-color: #ccc;
}
.toast_cls .content{
    display: flex;
    align-items: center;
}
.content .icon{
    font-size: 25px;
    color: #fff;
    height: 50px;
    width: 50px;
    text-align: center;
    line-height: 50px;
    border-radius: 50%;
    background: #2ecc71;
}
.toast_cls.offline .content .icon{
    background: #ccc;
}
.content .details{
    margin-left: 15px;
}
.details span{
    font-size: 20px;
    font-weight: 500;
}
.details p{
    color: #878787;
}
.toast_cls .close-icon{
    color: #878787;
    font-size: 23px;
    cursor: pointer;
    height: 40px;
    width: 40px;
    text-align: center;
    line-height: 40px;
    border-radius: 50%;
    background: #f2f2f2;
    transition: all 0.3s ease;
}
.close-icon:hover{
    background: #efefef;
}

Output

Detect Internet Connection using HTML CSS & JavaScript
Detect Internet Connection using HTML CSS & JavaScript

Now check you have successfully created a Toast Notification to Detect Internet Connection using HTML CSS & JavaScript. Note: If your code doesn’t work or facing any error/problem then please write a comment.

Code download

Categories:

CSSHTMLjQuery

Other Articles

How to delete tables from database when deactivate wordpress plugin
Previous

delete tables from the database when deactivating a plugin

Display Loading Image While Page Loads using jQuery
Next

How to Display Loading Image While Page Loads using jQuery

No Comment! Be the first one.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related Posts

Fix iOS Safari input type="file" Not Opening Camera - Real Solution

Fix iOS Safari input type=”file” Not Opening Camera – Real Solution

February 16, 2026
jQuery Validate Not Working After AJAX Loaded Form – Common Mistakes and Fix

jQuery Validate Not Working After AJAX Loaded Form – Common Mistakes and Fix

December 31, 2025
Jquery Validate Rules With if Condition

Jquery Validate Rules With if Condition

July 29, 2025
HTML Marquee tag

How to use HTML Marquee tag

January 23, 2025
Devnote

Devnote provides a collection of tutorials about PHP, Laravel, WordPress, Django, MySQL, Bootstrap, Jquery, Ajax, APIs, CRUD operations, etc.

Devnote © , All Rights Reserved.

Page Links

  • Home
  • About us
  • Blog
  • Contact US
  • Site map

Category

  • PHP
  • Laravel
  • wordpress
  • HTML
  • jQuery

Follow Us

Facebook Instagram Twitter Youtube Linkedin Pinterest
Morbi, Gujarat, India - 363641
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT
Advertisement