Introduction
In this blog and code example, we will use JavaScript code to check if the Internet connection is available or not on a machine. The JavaScript can be placed in a Web page.
JavaScript Navigator.OnLine property it used to check if the Internet connection is available on a machine or not. It returns a true value if the internet is available. It returns false of the internet is not available.
The following code example is an HTML web page that uses navigator.OnLine property on a button clicks to check the internet connection.
Example:
- <html>
-
- <head>
- <title>internet check</title>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
- </head>
-
- <body>
- <div class="container">
- <div class="row">
- <span>Click on the button to check the internet available or not</span> <button type="button" class="btn btn-success" onclick="internetCheck()">Try</button>
- </div>
- <span id="internetCheck"></span>
- </div>
- <script>
- function internetCheck() {
- var value = "Is the browser online? " + navigator.onLine;
- document.getElementById("internetCheck").innerHTML = value;
- }
- </script>
- </body>
-
- </html>
Summary
In this simple blog, we learned how can we create a Web app to check internet availability.