0

我有一个 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>

谢谢。

4

1 回答 1

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;
}
于 2013-04-28T11:41:01.453 回答