- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 −
Advertisements