3

我在 dashcode 中创建了一个嵌入 YouTube 视频的小部件。我想先测试互联网连接并提醒用户。我将 YouTube 小部件嵌入到 iBooks 中。我猜有时有些人不会有互联网连接。


如果我添加:

 var online = window.navigator.onLine;
if (!online) {
alert("we are offline");
//console.log("We are offline!");
} else {
alert("we are online");
//console.log("We are online!");
}

并将该代码作为小部件添加到 iBooks Author,弹出窗口工作正常,但无法确认警报。基本上,它锁定了 iBook。有任何想法吗?

4

1 回答 1

1

我不确定 ibook 仪表板,但我为我的网络应用程序编写了一个心跳检查器,可用于确认 http 连接,也许它可以做你需要的......原始帖子在这里

您使用要检查的 URL、最大 ttl 和回调调用代码。如果页面在 ttl 结束时(以毫秒为单位)没有响应,则使用 null 调用回调,否则您将获得状态和请求对象。

function heartbeat(url, ttl, callback) {
    // Confirms active connection to server by custom URL response
    //
    if (!url) {
        url = "http://www.yourwebsitehere.com/yourpage?someheartbeatcall";
        // Replace with specific server heartbeat location and query string for cache busting
    }
    if (!ttl) {
        ttl = 1000; // Custom timeout in milliseconds
        // Replace with specific server heartbeat location and query string for cache busting
    }
    // Create the Ajax object
    var ajaxRequest;
    try{
            ajaxRequest = new XMLHttpRequest();
    }
    catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e){
                // Unable to create
                callback(null);
                return;
            }
        }
    }
    // Set flag so only one pulse is recorded
    var called = false;
    // Make ajax call
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            if (!called) {
                called = true;
                callback(ajaxRequest.status, ajaxRequest);
            }
        }
    }
    ajaxRequest.open("GET", url, true);
    ajaxRequest.send(null); 
    // Make ttl timeout call
    var ttlcatch = setTimeout(function(){
        if (!called) {
            called = true;
            callback(null);
        }
    }, ttl);
    return;
}

var foo = false;
heartbeat("http://www.google.com", 1000, function(pulse){alert(pulse);} )
于 2012-04-09T01:05:47.407 回答