Code With Mark
Home
About
Resources
Contact

jQuery online & offline detection

Learn how to detect whether the browser is online or offline with jquery

Get the Code: 

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Network Connection Test</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.3/css/bootstrap.min.css" />
 

    <script>
    $(document).ready(function($) {
        
        //--->option > 1 > start
        
        $(window).bind('offline', function(event) {
            event.preventDefault();            
            $(document).find('.container_connection').html('<span class="text-danger">OFFLINE</span>');
        });

        $(window).bind('online', function(event) {
            event.preventDefault(); 
            $(document).find('.container_connection').html('<span class="text-success">ONLINE</span>');
        });        
        //--->option > 1 > end


        //--->option > 2 > start
        function check_internet_connection(){
            $.ajax({
                type: "GET",
                url: "https://dummyimage.com/300", 
                success: function(data, status, xhr) { 
                    console.log('status', xhr.status);
                },
                error: function(output) {

                    //console.log('OFFLINE');   
                    $(document).find('.container_connection').html('<span class="text-danger">AJAX > OFFLINE</span>');                
                }
            });
        };
 

        //run every 3 seconds
        setInterval(check_internet_connection, 3000);//ever 3 seconds 

        //--->option > 2 > end

    });
    </script>

</head>
<body style="background: black;">

    <div class="p-5"></div>
    <div class="container text-center online container_connection border"   style="font-size: 30px; width: 300px;border-radius: 25px;padding:15px; " >
        <span class="text-success">ONLINE</span>
    </div>
 
 
    
</body>
</html>
For Web Developers

Working Hard But Still Not Getting Ahead?

You finish a project, get paid, and then it's back to finding the next client.

Month after month, the cycle repeats.

That's why many web developers never build real financial freedom—even though they're highly skilled.

The developers creating long-term wealth are using those same skills to build SaaS products, plugins, and digital tools that generate recurring income.

What if your next project could pay you more than once?

Learn How To Build Monthly Income →
Under 10 Seconds Create HTML Tables With Search, Sort and More OptionsUnder 10 Seconds Create HTML Tables With Search, Sort and More Options←Previous
Mastering Form ValidationMastering Form ValidationNext→

Related Posts

  • Form Validation in 1 Line
  • Login System - Local Storage
  • Easily Edit HTML Table Rows Or Cells With jQuery

Top Posts Viewed

Google Sheets Timesheet Website App Script
1.98K views
Learn To Create YouTube Video Downloader
1.56K views
Easily Edit HTML Table Rows Or Cells With jQuery
1.2K views

Categories

Courses
Excel
Google Script
Javascript
jQuery
Microsoft Access
MongoDB
Node JS
PHP
Quick Tip
Uncategorized
Wordpress