当src不是有效的站点时,Safari不会调用iframe onload [英] Safari doesn't call iframe onload when src is not valid site

查看:128
本文介绍了当src不是有效的站点时,Safari不会调用iframe onload的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

我只是在前两天偶然发现了这个帖子,并提供了一个解决方案,以避免在Safari中加载事件。这篇文章是从2011年开始的,但似乎仍然有效。



Safari提供的解决方案是检查 readyState iframe 文档类似于:

 < iframe id =ifrasrc =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有关contentDocument的文档,您不需要在 contentWindow 当只在Safari中运行这个脚本时,但是我保留在这里以防万一你想跨浏览器使用它。


For the following iframe, Safari never calls the onload function and doesn't display anything in the iframe. All other browsers I've tested do call onload and display a default error web page.

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

Why is this happening? If there is no solution to this problem, then I need to find a way to detect if an iframe fails to load.

解决方案

I just the other day stumbled upon this post with a workaround for the missing load event in Safari. The post is from 2011 but seems to be still valid.

The solution proposed there for Safari is to check for the readyState event on the iframe's document like so:

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

As per Mozilla's documentation on contentDocument, you wouldn't need to include the workaround with the contentWindow when running this script only in Safari, but I kept it in here in case you want to use it across browsers.

这篇关于当src不是有效的站点时,Safari不会调用iframe onload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆