
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to detect whether the browser is online or offline with JavaScript?
To detect whether the browser is online or offline with JavaScript, the code is as follows −
Example
<!DOCTYPE html> <html> <body> <h1>Online or offline with JavaScript example</h1> <h2>Click the button below to check if you are online or not</h2> <button onclick="checkOnlineOffline()">Check online/offline</button> <h2 class="sample"></p> <script> function checkOnlineOffline() { if(navigator.onLine===true){ document.querySelector('.sample').innerHTML = "You are connected to internet" } else { document.querySelector('.sample').innerHTML = "You are not connected to internet" } } </script> </body> </html>
Output
The above code will produce the following output −
On clicking the “Check online/offline” button −
- Related Articles
- How to find whether a browser supports JavaScript or not?
- How to detect if JavaScript is disabled in a browser?
- How to check whether or not a browser tab is currently active using JavaScript?
- How can you detect the version of a browser using Javascript?
- How to detect the screen resolution with JavaScript?
- How to check the OffscreenCanvas is supported by the Browser or not in JavaScript?
- Execute a script when the browser starts to work offline in HTML?
- How to set whether or not an element is resizable by the user with JavaScript?
- How to set whether the style of the font is normal, italic or oblique with JavaScript?
- How to detect a mobile device with JavaScript?
- How To Run JavaScript Online?
- How to detect whether a Python variable is a function?
- How to detect the browser's format for HTML input type date
- How to check whether the background image is loaded or not using JavaScript?
- How to know the browser language and browser platform in JavaScript?

Advertisements