我有一个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>
感谢。
答案 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;
}