admin管理员组文章数量:1332339
Is there any way to deep clone iframe?
Basic jQuery cloning just makes another iframe with the same src. What I want to achieve is a way to clone the iframe and it's exact current content (ie. any possible input values, any DOM modifications through javascript etc.).
var clone = iframe.contents().find('html').clone();
When i clone the HTML attribute and all its children's, modifications etc I have these problems:
I'm missing the DOCTYPE
how do I insert it into the new iFrame?
Is there any way to deep clone iframe?
Basic jQuery cloning just makes another iframe with the same src. What I want to achieve is a way to clone the iframe and it's exact current content (ie. any possible input values, any DOM modifications through javascript etc.).
var clone = iframe.contents().find('html').clone();
When i clone the HTML attribute and all its children's, modifications etc I have these problems:
I'm missing the DOCTYPE
how do I insert it into the new iFrame?
- Missing the DOCTYPE shouldn't matter, AFAIK: it's a signal to the parser. Since you already have DOM elements, they shouldn't need to be parsed. – lonesomeday Commented Mar 14, 2012 at 13:15
1 Answer
Reset to default 4If you clone the iframe
creating another iframe
then you deep clone your iframe
! It's source is somewhere else and it'll be downloaded when you assign its src
attribute.
Test
Imagine you have as source a (non cached) page that prints the current time. Put an iframe
on your page, wait 10 seconds then clone it. The new iframe
will download its content and the 2nd iframe
will have a different content (the time).
"Real" deep cloning
EDIT: if you need a deep raw clone you may get the innerHTML and inject it inside the iframe. Solution from Michael Mahemof:
// Creates the new iframe, add the iframe where you want
var newframe = document.createElement("iframe");
document.body.appendChild(newframe);
// We need the iframe document object, different browsers different ways
var frameDocument = newFrame.document;
if (newFrame.contentDocument)
frameDocument = newFrame.contentDocument;
else if (newFrame.contentWindow)
frameDocument = newFrame.contentWindow.document;
// We open the document of the empty frame and we write desired content.
// originalHtmlContent is a string where you have the source iframe HTML content.
frameDocument.open();
frameDocument.writeln(originalHtmlContent);
frameDocument.close();
本文标签: javascriptHow to deep clone iframeStack Overflow
版权声明:本文标题:javascript - How to deep clone iframe? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742330129a2454523.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论