如何检测iframe中的错误404? [英] How to detect an error 404 in an iframe?

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

问题描述

我的网页使用iframe收集其他网页的内容。所有页面都在同一个域中。

My Web page uses iframes to collect content from other pages. All pages are in the same domain.

从主页面,有没有办法确认所有iframe都已加载,并且没有404错误?

From the main page, is there a way to confirm that all iframes have loaded, and that there is no 404 error?

推荐答案

状态仅存在于响应头中。



404页面正在处理 HTTP状态代码,这是仅包含在服务器发送给浏览器的响应中,但不包含在DOM的实际窗口文档对象中javascript可以访问。这意味着虽然您当然可以收集状态代码并采取适当的操作,但您可能只在javascript收到响应时执行此操作,例如使用 jQuery.ajax()request XmlHttRequest to加载您的iframe

The status only lives in the response header.

The 404 Page is handling an HTTP Status Code, which is only included in the server's response sent to the browser, but not in the actual window and document objects of the DOM that javascript may access. This means that while you certainly can collect the status-code and take appropriate actions, you may only do so when your javascript is receiving the response, such as with a jQuery.ajax() request or an XmlHttRequest to load your "iframe".

如果上面不是一个选项,唯一的另一种可能是检查404的标题和/或H标签。虽然这肯定不太理想(我很乐意看到,404,电影未找到,电影。),这是你唯一的另一种选择。

If the above isn't an option, the only other possibility may be to check the title, and/or H tags, for " 404 ". While this is most certainly less than ideal (I'd love to see, "404, Movie not Found, the Movie."), it is your only other option.

$('#iframe').load(function (e) {
    var iframe = $("#iframe")[0];

    if ( iframe.innerHTML() ) {
        // get and check the Title (and H tags if you want)
        var ifTitle = iframe.contentDocument.title;
        if ( ifTitle.indexOf("404")>=0 ) {
            // we have a winner! probably a 404 page!
        }
    } else {
        // didn't load
    }
});

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

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