Greetings to all! I make an application based on HTML5 + CSS3 + JS . The launch of this application starts from the language selection page, but since the application implies a link to the news feed, and sending information from the user to the creator, I need to restrict access to the main page if there is no connection to the network. Is it possible to check internet connection with JS ?

  • make a request and if you get an error - no connection - Grundy
  • I understand it! Request for what you need to perform? Does it connect to tape or ...? - Kirill
  • 2
    Yes, it is quite possible to add an error handler to connect to the news feed - Grundy

2 answers 2

I wanted to add that for your purposes you can use a ready-made solution. For example, Offline.js can be used as a simple library, or at the same time use the built-in functionality to display a message about no connection. Also in the mentioned lib there is a nice feature - it knows how to intercept all your requests to the server, and if one of them returns an error, then it immediately notifies you about it + starts caching all requests to the server. Thus, when the connection is fixed, it will be able to re-send all your requests to the server (of course, this can be turned off if necessary).

As for self-checking - in my opinion you should not check the availability of Google, if you need the availability of your site. Check the connection better by sending requests to some favicon of your site. Thus, you do not just check that the Internet is there, but also that your site has not crashed.

  • one
    An interesting thing! I will get acquainted necessarily. Thank you) - Kirill

Send a GET request to google.com , in the body of a test callback you can simply output something to the console.

var req = ({ method: 'GET', url: 'http://google.com' }); $http(req).then(function successCallback(successData){ //при удачном },function errorCallback(errorData){ //при ошибке }); 
  • Thank you So do) - Kirill
  • And if Google will fall? Or closes at all? - Darth
  • that's why I appeal to google, in my opinion this is simply impossible, but according to the theory of probability, there is a chance, although small, but there is. I answered the first thing that came to my head, but how to solve such a problem, a little higher a person has already said that this solution will work fine ( github.hubspot.com/offline/docs/welcome ) - Uladzimir Khadakouski