Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Testing for a Live Internet Connection in Javascript

Say the user has loaded a webpage in their browser from the hard drive, ie a ‘file:’ in the url. (Eg they might have created a microsoft mhtml file of my webpage.)

How can I check for a live internet connection?

[Edit: Someone can load an html file from their hard drive, but the page might still need a live connection for the page to function correctly.]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Loading from a file: URL doesn’t tell you anything about whether the user has a network connection.

If it’s specifically the file: URL thing that worries you, look at document.location.protocol, which will be file: or https: or http:, etc.

If you want to know if there’s a network connection, you can look at navigator.onLine (see the link for caveats) or do a fetch to a known-good server.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading