How to Check the Internet Connection Status with JavaScript | Async Await | Offline | Online

Ғылым және технология

Web Dev Roadmap for Beginners (Free!): bit.ly/DaveGrayWebDevRoadmap
In this video we'll discuss JavaScript Offline / Online / Internet Connection Detection methods and I'll provide a modern JS solution with Async / Await. All in less than 6 minutes!
📚 My article in #freeCodeCamp news: (includes async load event I forgot to put in video!)
www.freecodecamp.org/news/how...
✅ Quick Concepts outline:
• navigator.onLine
• online and offline events
• Seems good, but there is a problem
• CanIUse.com navigator.onLine notes
• MDN web docs on navigator.onLine
• A Better Modern JS Solution
📺 GitHub Gist for the code: gist.github.com/gitdagray/f31...
📚 Further Reading:
MDN Web Docs - navigator.onLine: developer.mozilla.org/en-US/d...
MDN Web Docs - setInterval: developer.mozilla.org/en-US/d...
MDN Web Docs - Async & Await: developer.mozilla.org/en-US/d...
✅ Follow Me:
Twitter: / yesdavidgray
Medium: / davegray_86804
#Javascript #Internet #Connection

Пікірлер: 31

  • @cameronthompson1275
    @cameronthompson12753 жыл бұрын

    Excellent video! Very clear, and easy to understand. Thanks!

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    3 жыл бұрын

    Glad it was helpful! Thank you.

  • @dharmeshgohil9375
    @dharmeshgohil93752 жыл бұрын

    THANK YOU

  • @thearmor9231
    @thearmor92312 жыл бұрын

    Thanks a lots!

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    2 жыл бұрын

    Glad I could help!

  • @maximumaim
    @maximumaim3 жыл бұрын

    Nice coding, thanks sir 😊

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    3 жыл бұрын

    You're welcome!

  • @rajashekarkothapelli9649
    @rajashekarkothapelli96493 жыл бұрын

    Hi, its a great video, will it shows network status when the wifi disconnected instead of unchecking offline check box in developer tools, Thanks

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    3 жыл бұрын

    Thanks! It should detect if it can download the 1 pixel image or not. If not, it will say offline. 👍

  • @shaiguy90
    @shaiguy90 Жыл бұрын

    This is great, extremely useful, and easy to understand. Great work! My only question/concern is that the fetch requests are blowing up my server logs. I assume there isn't a way to turn off logging just for this specific request...

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    Жыл бұрын

    Probably depends on your server and logging system. I know I could if I used logging middleware in Node.js. Glad the video helped!

  • @vladislavkarpov6372
    @vladislavkarpov63723 жыл бұрын

    Thank you for the explanation. I didn't get the trick with the 1pixel.png. Could you please explain this?

  • @vladislavkarpov6372

    @vladislavkarpov6372

    3 жыл бұрын

    Also, it worked with the browser online tick, but when I turned off wi-fi on my laptop it shows still Online.

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    3 жыл бұрын

    Hello Vladislav and thanks for asking. My explanation in the freeCodeCamp article is the best explanation I have: www.freecodecamp.org/news/how-to-check-internet-connection-status-with-javascript/

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    3 жыл бұрын

    @@vladislavkarpov6372 for your laptop online issue, I cannot diagnose from here. That said, if you read the provided freeCodeCamp article to understand the 1pixel image usage, this code should provide the results described :)

  • @CynthiaSotoCaballerolYEAHl

    @CynthiaSotoCaballerolYEAHl

    3 жыл бұрын

    @@DaveGrayTeachesCode Do I need to have the 1pixel.png image in my directory in order to work with this code? Because I'm getting a Not Found error 404

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    3 жыл бұрын

    @@CynthiaSotoCaballerolYEAHl You do need to have the 1pixel.png in the directory. The 404 indicates you do not have it there. This is just like needing your index.html file there or it will not be found.

  • @tictactoc9311
    @tictactoc93112 жыл бұрын

    Why are my fetch request remains return true even though I've already turned off my Wi fi?

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    2 жыл бұрын

    Are you getting a cached response? Check the network tab in dev tools. You might consider adding cache-busting to your fetch request: developer.mozilla.org/en-US/docs/Web/API/Request/cache

  • @tictactoc9311

    @tictactoc9311

    2 жыл бұрын

    Wow thanks👍 I didn't expect to get response lol

  • @maheshm4292
    @maheshm42923 жыл бұрын

    Wow

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    3 жыл бұрын

    I think that's a good wow 🙂 and if so, you're welcome.

  • @maheshm4292

    @maheshm4292

    3 жыл бұрын

    @@DaveGrayTeachesCode yes it's a good wow 😂

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    3 жыл бұрын

    @@maheshm4292 😂 Right on!

  • @thanhkhanhnguyen3850
    @thanhkhanhnguyen38502 жыл бұрын

    what about "How to Check the Website status Up or Down with JavaScript" ?

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    2 жыл бұрын

    You would need to be checking a different site. If your site was down, your Javascript would be down, too.

  • @ahmad-murery
    @ahmad-murery3 жыл бұрын

    I always check the network/router connection first and if succeeded I go and make a request to the server, this way I can return more accurate information about where the connection has failed (if any) Thanks

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    3 жыл бұрын

    I'm interested in seeing your code process for this, and is it something you do for all apps you deploy or only on your network?

  • @ahmad-murery

    @ahmad-murery

    3 жыл бұрын

    @@DaveGrayTeachesCode Thanks Dave, I remember I deployed one web app way back in the past, it uses XHR instead of fetch API, unfortunately it's not online anymore, maybe off-topic but I have implemented the same concept in a desktop DotNet app where the connection is not stable and we needed a way to inform the user about the server availability and whether the disconnection was due to a LAN issue or due to an internet issue (working-offline was not an option), it has a very close implementation to JS where we have a Network.IsAvailable property and a Network.NetworkAvailabilityChanged event listener which I used to update a global app status with corresponding value, in the same time another function is called periodically, it checks the global connection status and then request the server only if the network is available. I believe understanding the concepts is what makes you a better programmer and the code is just your tool to translate that to a working model regardless of the language. Sorry for the long reply.

  • @namsraikhatanbaatar9932
    @namsraikhatanbaatar99323 жыл бұрын

    Sass :D

Келесі