Check User’s Internet Connection With JavaScript
In the past, if the user enters our site depends on the Internet there was no harm in thinking. However, with the widespread use of HTML5-based hybrid mobile applications, we need to consider that the pages we create can be connected without an Internet connection. So if you want to run certain parts of your application only when you have the internet, I’ll show you a JavaScript feature that will allow you to do this in this article. Although I’m uncertain about the title of the article, today I think I’ll tell you the event meets.
navigator.online
Thanks to the navigator, we can easily access this information. When you look at the sample JavaScript code below, you can see what I mean more easily. navigator.online will return us connection status as true/false in boolean type.
Codes;
1 2 3 4 5 | if (navigator.onLine) { alert("connected."); } else { alert("not connected!"); } |
As you can see from the code, The control process is quite simple. You can use it in different ways by connecting it to a function if you want.
Note: the function returns true to you does not indicate that the user is always connected to the internet. When connected to a local network, the function returns “true”.