2013-04-28 62 views
0

我有一个iframe,但是当你没有互联网连接,它应该去 error.html。错误的iframe转到其他网站?

我的代码有什么问题?

<script> 
function errorsrc() { 
    document.getElementById['frame'].src = "error.html"; 
} 
</script> 

<iframe id="frame" frameborder="0" src="http://www.google.com/" onerror="errorsrc()" ></iframe> 

谢谢。

+0

即使网址无法加载onerror事件将不会触发。看到类似的问题http://stackoverflow.com/questions/3646079/how-to-check-if-iframe-fails-to-load-jquery – 2013-04-28 08:46:09

+0

当没有互联网连接,你怎么去error.html? – 2013-04-28 09:59:07

+0

@KhanhTo error.html处于脱机状态 – eds1999 2013-04-28 13:29:51

回答

0

可以使用脆弱navigator.onLine检测到任何在线/离线状态:

var isOnline = getOnlineStage(); // true/false. Is null if property is not supported. 
if (isOnline === false) { 
    // do your offline stuff here... 
} 

function getOnlineStage() { 
    var nav = window.navigator, 
     hasOwn = Object.prototype.hasOwnProperty; 

    return hasOwn.call(nav, 'onLine') ? nav.onLine : null; 
} 
相关问题