jcubic
jcubic

Reputation: 66650

Test if browser have internet connection using HTML5

Is there any new HTML5 API, I couldn't find anything, that will check if user have internet connection. Or maybe there is unusual use of some API that will allow to do this. I had to test it and I've used ajax call to test, is there other method?

Upvotes: 1

Views: 2914

Answers (1)

Alnitak
Alnitak

Reputation: 340055

In some new browsers there is the navigator.onLine property and the online and offline events. See https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine.onLine

Failing that, you could try an Image element instead of AJAX (assuming you can find a consistent image URL that won't disappear). This has the advantage of not needing to worry about cross-origin security limitations that make it hard to distinguish a failed AJAX call from one that was blocked because of CORS issues.

Upvotes: 4

Related Questions