views:

203

answers:

1

After opening an iframe, I sometimes need to close it. Thus far, I just remove the iframe from its parent node, thus cutting it off from the DOM. However, I'm finding that this doesn't immediately end the iframe's life, and its javascript code can still call back into the parent window, which no longer expects these callbacks.

How can I kill an iframe dead? I don't see a close or destroy method on it.

+4  A: 

Create a blank.htm file on your site. Have the IFrame SRC set to the blank.htm, then remove the IFRAME.

AnthonyWJones
I guess I could send the iframe to blank.htm even if that doesn't exist on my site, since it's a hidden iframe and the 404 wouldn't matter. Thanks.
Andrew Arnott
The 404 would be more expensive, usually 404 has a significant response body and even references to images etc. They are also not cacheable. Using an empty Blank.htm would likely result in no roundtrip to the server at all or a cheaper one than 404.
AnthonyWJones
Good point. What about "about:blank"? I tried that on IE and Chrome, and they both handle it, without any call to the server.
Andrew Arnott
Yes about:blank will probably work in most circumstances but it might trip you up if ever want to serve your site over HTTPS, you might get "some content insecure" warning message boxes.
AnthonyWJones