重复iframe:将头部和身体从一个iframe复制到另一个ifame
Duplicate iframe: Copy head and body from 1 iframe to another
我似乎找不到答案的简单问题:我在一页上有两个iframe,我想把第一个的内容复制到第二个。但是,我不能只将第一个iframe的url复制到第二个iframe,因为包含的页面是动态的。
这段代码确实做到了,但很多页面格式似乎都丢失了。我也不知道它是否是跨浏览器的。
iframe2.contentWindow.document.write(iframe1.contentWindow.document.body.innerHTML);
这能做到吗?
要求的本地JavaScript解决方案:
首先,为了简单起见,我创建了两个对象文字:
var iframe1 = {
doc : undefined,
head : undefined,
body : undefined
};
var iframe2 = {
doc : undefined,
head : undefined,
body : undefined
};
接下来,我将所有内容放在iframe1的window.onload
处理程序下,以确保它已完全加载:
document.getElementById("iframe1").contentWindow.onload = function() {
然后我分配了所有的对象文字属性:
iframe1.doc = document.getElementById("iframe1").contentWindow.document;
iframe1.head = iframe1.doc.getElementsByTagName("head")[0];
iframe1.body = iframe1.doc.getElementsByTagName("body")[0];
iframe2.doc = document.getElementById("iframe2").contentWindow.document;
iframe2.head = iframe2.doc.getElementsByTagName("head")[0];
iframe2.body = iframe2.doc.getElementsByTagName("body")[0];
接下来,我需要创建两个函数removeNodes()
和appendNodes()
,这样我就可以重新考虑一些用于<head>
和<body>
例程的代码。
function removeNodes(node) {
while (node.firstChild) {
console.log("removing: " + node.firstChild.nodeName);
node.removeChild(node.firstChild);
}
}
和:
function appendNodes(iframe1Node, iframe2Node) {
var child = iframe1Node.firstChild;
while (child) {
if (child.nodeType === Node.ELEMENT_NODE) {
console.log("appending: " + child.nodeName);
if (child.nodeName === "SCRIPT") {
// We need to create the script element the old-fashioned way
// and append it to the DOM for IE to recognize it.
var script = iframe2.doc.createElement("script");
script.type = child.type;
script.src = child.src;
iframe2Node.appendChild(script);
} else {
// Otherwise, we append it the regular way. Note that we are
// using importNode() here. This is the proper way to create
// a copy of a node from an external document that can be
// inserted into the current document. For more, visit MDN:
// https://developer.mozilla.org/en/DOM/document.importNode
iframe2Node.appendChild(iframe2.doc.importNode(child, true));
}
}
child = child.nextSibling;
}
创建了这些函数后,现在我们所要做的就是调用:
console.log("begin removing <head> nodes of iframe2");
removeNodes(iframe2.head);
console.log("begin removing <body> nodes of iframe2");
removeNodes(iframe2.body);
console.log("begin appending <head> nodes of iframe1 to iframe2");
appendNodes(iframe1.head, iframe2.head);
console.log("begin appending <body> nodes of iframe1 to iframe2");
appendNodes(iframe1.body, iframe2.body);
最后,我们关闭了window.onload
函数:
};
相关文章:
- 在iframe中加载url并添加一个类
- 如何使用JS/jQuery在另一个网站上显示iframe中的特定内容
- 将一个iframe覆盖在另一个ifame上
- 在单击一个或多个复选框的同时调用iframe
- 从一个iframe到另一个ifame的过帐表单-IE
- 我如何才能包含一个iframe视频,这样它就不会't负载
- 如何从一个友好的iframe中获取iframe实例
- 代理一个网站能够在iframe中进行所见即所得编辑-它是如何工作的
- 在html页面的iframe中显示一个警告框
- 从另一个 Iframe 刷新父 Iframe
- 当我有一个完整的html页面时,如何显示它's iframe中的源html
- 加载一个html或JavaScript文件作为iframe源
- 如何从另一个域访问 iframe 的元素
- 按日期为iframe创建一个HTML链接
- 如何将iFrame与带有'#'在一个简单的模态框中
- Ajax下拉/最后一个选项更改Iframe
- 有没有一种方法可以用一个请求加载多个iframe
- 是否可以打开一个新窗口并将iframe嵌入其中
- 一个iframe随机失败到XHR
- 我必须iframe一个父级和另一个子级,我想使用jQuery获取子iframe值,我怎么能得到