Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect an error 404 in an iframe?

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

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

like image 736
Christophe Avatar asked May 11 '13 16:05

Christophe


People also ask

What triggers a 404 error?

404 error or 'page not found' error is a Hypertext Transfer Protocol standard response code that indicates the server was unable to find what was requested. This message may also appear when the server is not willing to disclose the requested information or when the content has been deleted.

How do I fix status code 404?

The simplest and easiest way to fix your 404 error code is to redirect the page to another one. You can perform this task using a 301 redirect. What's 301, you may ask? It's a redirect response code that signals a browser that the content has been transferred to another URL.

Does an HTTP status code of 404 mean?

404 is a status code that tells a web user that a requested page is not available. 404 and other response status codes are part of the web's Hypertext Transfer Protocol response codes. The 404 code means that a server could not find a client-requested webpage.


1 Answers

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

Hope the 404 page follows 404 standards.

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     } }); 
like image 62
Tony Chiboucas Avatar answered Sep 25 '22 09:09

Tony Chiboucas