iBooks通过HTML小部件测试互联网连接



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


如果我加上:

 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。有什么想法吗?

我不确定ibook仪表板,但我为我的web应用程序编写了一个心跳检查器,可以用来确认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);} )

最新更新