ユーザーがアクティブなインターネット接続を持っているかどうかを確認するために、次のリスナーがあります。
$(function() {
$( window ).bind(
"online offline",
function( event ){
console.log("-------------------window bind------------------------------");
if(hasInternets()){
console.log("window bind online---------------------------------------------");
sendAllPendingData();
}else {
console.log("window bind offline--------------------------------------------");
}
}
);
});
function hasInternets() {
console.log("hasInternets: " + window.location.href.split("?")[0] + "?" + Math.random());
var s = $.ajax({
type: "HEAD",
url: window.location.href.split("?")[0] + "?" + Math.random(),
async: false
}).status;
console.log("s: " +s);
//thx http://www.louisremi.com/2011/04/22/navigator-online-alternative-serverreachable/
return s >= 200 && s < 300 || s === 304;
}
私のラップトップでは問題なく動作します。他のマシンやデバイスで正常に動作するかどうかはわかりません。私はこのことについて詳しくありません。誰かがアドバイスできるなら、本当に感謝します。ところで、調査を行っているときに、jquery プラグインでこの TrueOnline を見つけました。誰か試したことがありますか? http://archive.plugins.jquery.com/project/trueonline
ありがとう。