是活動我有檢查,如果用戶有一個有效的互聯網連接下面的聽衆:檢查,如果互聯網的使用jQuery
$(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
謝謝。
可能重複[檢查Internet連接是否存在Javascript?](http://stackoverflow.com/questions/2384167/check-if-internet-connection-exists-with-javascript)<可能有幫助> **' :) ** – 2012-07-18 09:05:30
嗨,tats。謝謝回覆。但是恐怕我需要一個用於離線/在線事件的監聽器,而不僅僅是用於在頁面加載時檢查用戶是否在線/離線。 – lorraine 2012-07-18 15:23:29