views:

1612

answers:

1

It sounds wonky, but this is what I'm trying to do with javascript (this is all triggered by an event handler):

  1. Save the contents of a page (preferably the whole document or at least documentElement object) into a variable.
  2. Create an iframe and insert it into the body.
  3. Replace the document of the iframe with the saved document (so that the iframe now has the same content as the outer page).
  4. Change the outer page.

Everything is dandy except the part about replacing the iframe's document. I've tried a bunch of different things, but all have problems (they are outlined below). I need event handlers, styles, links, etc to all work correctly when the page is in the iframe.

And yes, I realize that I could just set the src of the iframe to the url of the current page, but I'd rather the page not have to load. If I can't come up with a solution, this is what I'll do.


Things that I've already tried:

var html = iframe.contentWindow.document.importNode(document.documentElement, true);
iframe.contentWindow.documentElement = html;

I get an error message ("setting a property that only has a getter").

var html = iframe.contentWindow.document.importNode(document.documentElement, true);
var idoc = iframe.contentWindow.document;
while(idoc.firstChild) idoc.removeChild(idoc.firstChild);  // remove all children (firefox automatically creates an html element for iframes with no src)
idoc.appendChild(html); // insert a new documentElement

This seems to work, but doesn't re-render the styles, and event handlers don't work. Interestingly, links don't work either, and they aren't even styled with the browser's default styles. Maybe this is because the DOCTYPE isn't copied to the iframe?

iframe.contentWindow.document.documentElement.innerHTML = document.documentElement.innerHTML;

This has the same behavior as the removeChild/appendChild method.


It seems like if I could just get the browser to re-render the styles and re-register the event handlers then I'd be golden, but I'm not sure if this is even possible.

+1  A: 

In regards to the last method, perhaps the DOM of the window you are copying FROM has not loaded yet?

As for the Doctype, you could rebuild it from scratch and insert it before the html node using a document.write.

The doctype is accessible through document.doctype , but this only has a getter.

I create the iframe when Prototype's "dom:loaded" event fires and I copy the document in a "click" event. I'll try the doctype thing and see if it works, though.
Matt Kantor
I'm trying to put the doctype in, but I'm having trouble using document.write with document.appendChild. I've tried putting document.close() all over the place but nothing seems to work.
Matt Kantor
Off the top of my head, the doctype wouldn't be added using appendChild, as the DocType itself is a comment rather than a node, I could be wrong on this though.I would build the document.type in a string from the other frame's doctype. Then I would copy using document.write in the form of..
document.write(docTypevariable+"<html>"+ ..documentElement.innerHTML+"</html>