iBooks测试与HTML小部件的互联网连接

时间:2012-01-28 04:16:19

标签: javascript dashcode ibooks

我在dashcode中创建了一个嵌入YouTube视频的小部件。我想首先测试一个interent连接并提醒用户。我将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。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

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

您可以使用URL来调用代码来检查,max 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);} )