Move IFRAME DOM to new window

1.3k views Asked by At

Despite is it not useful to move an IFRAME to a newly opened window (see similiar questions, the IFRAME will reload), one can move the IFRAMEs content, eg. the top HTML node to another window. Eg.

iframe_html_node=document.getElementById('my_iframe').contentDocument.firstChild;
new_window=window.open();
new_window.document.replaceChild(iframe_html_node,new_window.document.firstChild);

Of course, one can also replace deeper levels of the DOM if only parts are needed.

However, there is one show stopper: As the new window is opened blank, it has no valid document.location set, so there is no base URI for it's contents links and ressources, which will in turn all go blank (tested in Chrome), eg. CSS and images are broken.

One can of course set new_window.document.location to the IFRAMEs one before copying, but this triggers a lot of mess if included JS frameworks attach their handlers and so on. Also it implies an race condition where we have to be cautionous about the time we replace the HTML node, as it may be overwritten by the loading document.

Any suggestions?

0

There are 0 answers