What is the best way to detect if there's internet connection or not on my mobile via my web app?
相关问题
- Views base64 encoded blob in HTML with PHP
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
in simple JS code this can done, causation all featured devices not have JS supported however for web-based application this is very minimum code to use
if you want to check every X seconds the connection.
There's no code necessary for this -- it's part of the HTML5 API. Check the value of
window.navigator.onLine
-- it will befalse
if the user is offline.http://www.whatwg.org/specs/web-apps/current-work/multipage/offline.html#browser-state
An option might be changing the Ajax settings to add a specific
timeout
, then add anerror
handler that looks for atextStatus
(second argument) of'timeout'
.When a timeout occurs, either internet connectivity is spotty or your site is down.
Using ajaxSetup to set option defaults for all requests:
If you want something with more compatibility, reliability and customisability than window.navigator.onLine, try my jQuery plugin: http://tomriley.net/blog/archives/111