Check network connectivity using javascript

Posted on Updated on

In Modern web application form validation and lots of business logic related with UI resides on client side which is nothing but our browsers. User registration is first step where customer enter personal and other detail based on type of application. Suppose while filling up the registration form network connectivity lost and user submitted the form. In that scenario registration fails and user has to re-fill the form and submit again.

The solution of this problem is that before submitting the user registration form, developer should check whether connection are available or not. If connectivity lost, developer should store data locally and submit the data once connections are available.

Below example demonstrate connectivity check using javascript.

navigator.onLine is javascript method which is associated with browser let us know the current status of network. In above example we have created updateStatus() method which update the label with current state. Also window events are added so to update the status in case availability of network and vice-versa.

Output
1) When device connected with data network

online

2) When device doesn’t connected with data network

offline

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.