Detect Internet Connection with JavaScript

Sometime, it becomes necessary to know about status of internet connectivity. We can know about internet connectivity in PHP also, but it is not good practice.

Using client side language to detect internet connectivity is good practice because it gives connection status without reloading a page.

So, using browsers property (navigate.online) we can easily confirm the status of Internet connection. It returns online status of the browser.

Let’s us see snippet.

HTML

<button onclick="internetStatus();">Check Connection</button>   

JavaScript

<script>     
function internetStatus()
{
if(navigator.onLine)
{
alert("You are online");
}else
{
alert("You are offline");
}
}
</script>

View Demo

[sociallocker] Download[/sociallocker]

Leave a Comment