Tuesday, February 16, 2010

302 redirects and iframe

I was quite surprised to discover that the behavior of posting a form while targeting an iframe depends on whether the form handler performs an HTTP 302 redirect.

I have a web page, with an iframe embedded and a form that targets that iframe. If I post the form and the handler returns a normal 200 result, the iframe will end up with the content sent by the handler, while keeping the surrounding content intact. That's the behavior that I was intuitively expecting.

However if the form handler returns an 302 code, instructing the browser to do a redirect, the whole page gets redirected, so the iframe and surrounding content vanish, being replaced by whatever content is served by the redirection.

This is quite surprising to me, as I would have expected only the target iframe to be affected by the posting of the form. After all that's the semantic of the target, isn't it ? This odd behavior happens in both FireFox and Safari on Mac, so it may not be a bug, but by design. I'd be curious about the rationale behind this.

No comments: