howto replace document object of a window/iframe

北城以北 提交于 2019-12-01 03:20:27

问题


I need to inject in an iframe window a document object that I instanciated previously, and I cannot serialize it into a string or a remote url (those are solutions proposed on previous stackoverflow posts), because elements of this document objects are bound to other objects in my code.

How can I do it ?

thanks, b.


回答1:


Try using importNode:

/* Change these: */
var documentToCopy = document,
    iframeDocument = iframe.contentWindow.document;

/* Replace current document-element (<html>) with the new one: */
iframeDocument.replaceChild(
    iframeDocument.importNode(documentToCopy.documentElement, true),
    iframeDocument.documentElement
);

See https://developer.mozilla.org/en/DOM/document.importNode



来源:https://stackoverflow.com/questions/1356942/howto-replace-document-object-of-a-window-iframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!