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

Why Are You Still Getting Paid Only Once?

You spend weeks building a project.

Your client pays you.

Then the income stops.

Meanwhile, other developers are turning similar skills into products that generate revenue month after month.

A SaaS, plugin, web app, or digital product can continue bringing in customers long after it's launched.

The real question isn't whether you can build one.

It's how much money you're leaving on the table by not starting.

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

Easily Edit HTML Table Rows Or Cells With jQuery
366 views
How To Create A Secure Login System With PHP And MySQL
262 views
PHP Simple Database Class
255 views

Categories

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