【问题标题】:iframe on error go to other site?iframe 出错转到其他站点?
【发布时间】:2013-04-28 08:04:21
【问题描述】:

我有一个 iframe,但是当你没有互联网连接时,它应该转到 错误.html。

我的代码有什么问题?

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

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

谢谢。

【问题讨论】:

  • 即使 url 加载失败,onerror 事件也不会触发。查看类似问题stackoverflow.com/questions/3646079/…
  • 在没有网络连接的情况下,怎么去error.html?
  • @KhanhTo error.html 离线..

标签: javascript html iframe error-handling web


【解决方案1】:

您可以使用脆弱的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;
}

【讨论】:

    猜你喜欢
    • 2018-09-30
    • 2012-06-05
    • 2014-02-15
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 2017-11-03
    • 2017-12-19
    • 1970-01-01
    相关资源
    最近更新 更多