2013-04-28 58 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; 
} 
相關問題