gpt4 book ai didi

javascript - 当 src 不是有效站点时,Safari 不会调用 iframe onload

转载 作者:行者123 更新时间:2023-11-29 18:16:01 28 4
gpt4 key购买 nike

对于下面的 iframe,Safari 不会调用 onload 函数,也不会在 iframe 中显示任何内容。我测试过的所有其他浏览器都会调用 onload 并显示默认错误网页。

<html>
<body>
<iframe src="http://www.asdkjhjhkjhkjhjhjhfhkjhsdflkjahdsjfhasf.com"
onload="alert('iframe loaded');">
</iframe>
</body>
</html>

为什么会这样?如果这个问题没有解决方案,那么我需要找到一种方法来检测 iframe 是否加载失败。

最佳答案

我前几天偶然发现了this post Safari 中缺少 load 事件 的解决方法。该帖子来自 2011 年,但似乎仍然有效。

编辑:上面的链接似乎不再有效。这是一个cached copy

那里为 Safari 提出的解决方案是检查 iframedocument 上的 readyState 事件,如下所示:

<iframe id="ifra" src="http://www.asdkjhjhkjhkjhjhjhfhkjhsdflkjahdsjfhasf.com"
onload="alert('iframe loaded');">
</iframe>
<script>
var iframe = document.getElementById('ifra'),
doc = iframe.contentDocument || iframe.contentWindow;
if (doc.document) doc = doc.document;
var _timer = setInterval(function() {
if (doc.readyState == 'complete') {
clearInterval(_timer);
alert('iframe ready');
}
}, 1000);
</script>

根据 Mozilla's documentation on contentDocument ,当仅在 Safari 中运行此脚本时,您不需要将解决方法包含在 contentWindow 中,但我将其保留在此处以防您想跨浏览器使用它。

关于javascript - 当 src 不是有效站点时,Safari 不会调用 iframe onload,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23355179/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com