Javascript 如果帧出错,是否转到其他站点?

Javascript 如果帧出错,是否转到其他站点?,javascript,html,iframe,error-handling,web,Javascript,Html,Iframe,Error Handling,Web,我有一个iframe,但是当您没有internet连接时,它应该转到 error.html 我的代码怎么了 <script> function errorsrc() { document.getElementById['frame'].src = "error.html"; } </script> <iframe id="frame" frameborder="0" src="http://www.google.com/" onerror="errorsr

我有一个iframe,但是当您没有internet连接时,它应该转到 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>

函数errorsrc(){
document.getElementById['frame'].src=“error.html”;
}
谢谢。

您可以使用易碎品检测任何开/离线状态:

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;
}

即使加载url失败,onerror事件也不会触发。在没有internet连接的情况下,请参阅类似的问题,如何转到error.html?@KhanhTo error.html处于脱机状态。。