0

I just want to replace a blank iframe's document with a pre-existing document object. Not a string of HTML that can use the write() method with (that solution is in many other answers, here). I just want a document object to replace another document.


My blank iframe:

<iframe src="about:blank" id="myIframe"></iframe>


Routine to replace blank iframe document:

function replaceIframeDoc(objDoc){
    var nod_if    = document.getElementById("myIframe");
    var nod_ifDoc = nod_if.contentWindow.document || nod_if.contentDocument;
    nod_ifDoc     = objDoc;
}


Document object created, and call to do the replace:
(Note: I encapsulate my document object in a simple object... I need to do that for some other unrelated stuff... just to easily transfer some attributes, etc. Hopefully it's inconsequential to my issue!)

var obj_container = new Object;  //just to make easier to do some other stuff
obj_container.document = document.implementation.createHTMLDocument();
obj_container.document.open();
obj_container.document.write("<html>...(some html)...</html>");
obj_container.document.close();
replaceIframeDoc(obj_container.document);
csr19us
  • 105
  • 9
  • I think you could follow this idea: http://stackoverflow.com/questions/18196541/how-to-create-a-copy-of-a-form-with-its-dom-elements – emerson.marini Jun 16 '14 at 15:56

1 Answers1

0

Depending on which browser you need to support, you could do something like this:

var iframe = document.getElementById("myIframe");;
var oldNode = iframe.contentDocument.getElementById("myNode");
var newNode = document.importNode(oldNode, true);
document.getElementById("container").appendChild(newNode);

You can read more here: https://developer.mozilla.org/en-US/docs/Web/API/document.importNode

Brian Lewis
  • 5,739
  • 1
  • 21
  • 28
  • I need to primarily support webkit and safari. `importNode()` seems like the best method... however, I'm not sure it's really suited for the document-level element, is it? – csr19us Jun 16 '14 at 17:39