cdugga
cdugga

Reputation: 3859

Iframe and http error when loading invalid or protected URL

IM writing some html which provides a preview function for a list of URLs. I want to use an iframe for this functionality,

The issue arises when some of the URLs are broken (returning a 500 error) or when a page contains some authenication process which the requesting user cannot satisfy. In these situations the iframe is trying to display the URL but the content returned in the frame is useless (500 error or authenication error ) to the user.

DOes iframe have any built in error handling for these senarios or is there some other way i can display a generic error page if something happens when loading the iframe?

Thanks

Upvotes: 1

Views: 2706

Answers (1)

DaveRandom
DaveRandom

Reputation: 88697

AFAIK, there is no way to directly access the header of a response to a request initiated by an iframe (or indeed, any request) in client side script.

This is slightly convoluted, but I think it would work:

  1. The iframe is initially loaded with a URL that refers to a script on your server, and your pass the actual URL as a GET parameter.
  2. The server side script takes that URL, and sends a HEAD request to it (following 3xx redirects).
    • If the response code for the HEAD request is >= 200 and < 300, send some script back to the client which changes the iframe's src to the actual URL (you might be able to do something as simple as window.location.href = but I'm not sure without testing).
    • If the response code is >= 400, send a page that says "This page is not loading at the moment".

If you know PHP/have it available on your server, I can try and provide a code example.

Upvotes: 1

Related Questions