检测iframe加载错误 [英] detect iframe load error

查看:157
本文介绍了检测iframe加载错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用src属性将用户选择的页面加载到iframe中。如果加载失败,我想以对用户有意义的方式报告问题。根据 http://www.w3schools.com/jsref/dom_obj_frame.asp

I am loading a user-selected page into an iframe using the src property. If the load fails, I would like to report the problem in terms that will make sense to the user. iframe does not, in general, support onerror according to http://www.w3schools.com/jsref/dom_obj_frame.asp.

该页面可能来自用户的域名,而不是我的域名,因此我无法查看iframe的内容。

The page may come from the user's domain, not mine, so I cannot view the content of the iframe.

如果加载成功,我可以设置超时并从我的onload处理程序取消它,但是需要长时间超时才能避免错误的错误报告,同时我的iPhone上的Safari已显示警告混淆用户。即使这对Kindle Fire浏览器也不起作用 - 无论加载是否成功,它都会向我的处理程序发送一个加载事件。

I can set a timeout and cancel it from my onload handler if the load is successful, but it would need to be a long timeout to avoid false error reports, and meanwhile Safari on my iPhone has displayed an alert that may confuse the user. Even this does not work for the Kindle Fire browser - it delivers a load event to my handler regardless of whether the load was successful.

我是否可以使用任何事件检测失败?有没有办法抑制默认的Safari行为?我可以用任何方式判断加载尝试是否失败了? (如果我可以这样做,我可以使用更短的超时和轮询,直到加载尝试得到解决)。

Is there any event I can use to detect failure? Is there any way to suppress the default Safari behavior? Any way I can tell whether the load attempt has failed? (If I could do that, I could use a shorter timeout and poll until the load attempt is resolved).

我可以要求使用最新的浏览器,但是想要一个可以在尽可能多的智能手机和平板电脑中移植的解决方案。

I can require the use of up to date browsers, but would like a solution that is portable among as many smartphones and tablets as possible.

我已经测试了AJAX Get的想法,但遗憾的是它不起作用。跨域AJAX获取任意URI会导致异常,无论目标是否存在且可以加载到iframe中。

I have tested the AJAX Get idea, and it unfortunately does not work. A cross-domain AJAX Get to an arbitrary URI results in an exception, regardless of whether the target exists and can be loaded into the iframe or not.

推荐答案

您可以将iframe和/或ajax请求设置为始终调用您控制的页面(即:loader.php),通过get发送loader.php用户请求的页面。从loader.php,使用curl甚至只是file_get_contents来获取外部页面。如果请求无法返回到loader.php,您可以检查错误,并返回您想要显示的iframe。

You could set your iframe and/or ajax request to always call a page you control (ie: loader.php), sending loader.php the user's requested page via get. From loader.php, use curl or even just file_get_contents to fetch the external page. If the request fails to come back to loader.php, you can check the error there, and return whatever you want your iframe to display.

虽然我的例子引用了php的使用,但各种脚本语言都支持curl。它可能比您可能拥有的其他解决方案更复杂,但是可以让您访问响应标头以及排除页面加载失败的原因。

While my example references the use of php, curl is supported in a variety of scripting languages. It is likely more complicated than other solutions you might have, but would give you access to the response headers as well for troubleshooting why a page load failed.

这篇关于检测iframe加载错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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