错误的iframe转到其他网站?

时间:2013-04-28 08:04:21

标签: javascript html iframe error-handling web

我有一个iframe,但是当你没有互联网连接时,它应该去 的error.html。

我的代码出了什么问题?

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

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

感谢。

1 个答案:

答案 0 :(得分: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;
}