页面中包含Iframe,不排除多层嵌套包含。期望得到完整HTML树,结构如下:<html>
<head>
...
</head>
<body>
<iframe>
<html>
<head>
...
</head>
<body>
<iframe>
...
</iframe>
</body>
</html>
</iframe>
</iframe>...</iframe>
</body>
</html>左右搞搞一天没搞出个好方案,需要兼容Firefox与IE。IE内里元素的attributes把我搞晕了,每个元素一迭代一百多个多项目
<head>
...
</head>
<body>
<iframe>
<html>
<head>
...
</head>
<body>
<iframe>
...
</iframe>
</body>
</html>
</iframe>
</iframe>...</iframe>
</body>
</html>左右搞搞一天没搞出个好方案,需要兼容Firefox与IE。IE内里元素的attributes把我搞晕了,每个元素一迭代一百多个多项目
估计我没描述清晰,关键是必须使用JavaScript读取当前页面,请勿跑题。
跨域用首次HTTP请求代理,注意是首次。跨域等问题 不在本论题之内。
window.onload = function(){
alert(document.getElementsByTagName("html")[0].innerHTML);
}
而 FF 似乎不行,不知是否能找到与 document.frames 对应的集合?!IE 应该没问题,代码明早上班就写,回家吃饭先,88
而 FF 似乎不行
p1
p2
p3
p3
即 p1 里嵌入了 p2、p3,p2 里嵌入了 p3。代码如下:L@[email protected]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
</head> <body>
<h1>页面 1 包含 页面 2</h1>
<iframe id="iframe12" frameborder=0 scrolling=no src="p2.html"></iframe> <h1>页面 1 包含 页面 3</h1>
<iframe id="iframe13" frameborder=0 scrolling=no src="p3.html"></iframe>
<script type="text/javascript" defer>
<!--
function getFullHTML(doc)
{
var fullHTML;
var aIframeHTML = new Array(); for (var i=0; i<doc.frames.length; i++)
{
aIframeHTML.push(getFullHTML(doc.frames[i].document));
} fullHTML = doc.documentElement.outerHTML; if(aIframeHTML.length > 0)
{
var regIframe = /<\/iframe>/gi;
var ind = 0;
fullHTML = fullHTML.replace(regIframe, function($0) {
return aIframeHTML[ind++] + $0 + "\n\n";
});
} return fullHTML;
}alert(getFullHTML(document));
//-->
</script>
</body>
</html>
p2.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
</head> <body>
<h2>页面 2 包含 页面 3</h2>
<iframe id=iframe23 frameborder=0 scrolling=no src="p3.html"></iframe>
</body>
</html>
p3.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
</head> <body>
<h3>页面 3 被页面 2 包含!</h3>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
</head> <body>
<h1>页面 1 包含 页面 2</h1>
<iframe id="iframe12" frameborder=0 scrolling=no src="p2.html"></iframe> <h1>页面 1 包含 页面 3</h1>
<iframe id="iframe13" frameborder=0 scrolling=no src="p3.html"></iframe>
<br />
<textarea id="ttaOutput" rows="20" cols="60"></textarea> <script type="text/javascript" defer>
<!--
function getFullHTML(doc)
{
var fullHTML;
var aIframeHTML = new Array(); for (var i=0; i<doc.frames.length; i++)
{
aIframeHTML.push(getFullHTML(doc.frames[i].document));
} fullHTML = doc.documentElement.outerHTML; if(aIframeHTML.length > 0)
{
var regIframe = /<\/iframe>/gi;
var ind = 0;
fullHTML = fullHTML.replace(regIframe, function($0) {
return aIframeHTML[ind++] + $0 + "\n\n";
});
} return fullHTML;
}document.getElementById("ttaOutput").value = getFullHTML(document);
//-->
</script>
</body>
</html>
最终输出代码如下:<HTML xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>new document</TITLE>
<META content=editplus name=generator>
<META content="" name=author>
<META content="" name=keywords>
<META content="" name=description></HEAD>
<BODY>
<H1>页面 1 包含 页面 2</H1>
<IFRAME id=iframe12 src="p2.html" frameBorder=0 scrolling=no>
<HTML xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>new document</TITLE>
<META content=editplus name=generator>
<META content="" name=author>
<META content="" name=keywords>
<META content="" name=description></HEAD>
<BODY>
<H2>页面 2 包含 页面 3</H2>
<IFRAME id=iframe23 src="p3.html" frameBorder=0 scrolling=no>
<HTML xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>new document</TITLE>
<META content=editplus name=generator>
<META content="" name=author>
<META content="" name=keywords>
<META content="" name=description></HEAD>
<BODY>
<H3>页面 3 被页面 2 包含!</H3></BODY>
</HTML>
</IFRAME>
</BODY>
</HTML>
</IFRAME><H1>页面 1 包含 页面 3</H1>
<IFRAME id=iframe13 src="p3.html" frameBorder=0 scrolling=no>
<HTML xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>new document</TITLE>
<META content=editplus name=generator>
<META content="" name=author>
<META content="" name=keywords>
<META content="" name=description></HEAD>
<BODY>
<H3>页面 3 被页面 2 包含!</H3></BODY>
</HTML>
</IFRAME><BR><TEXTAREA id=ttaOutput rows=20 cols=60></TEXTAREA>
<SCRIPT defer type=text/javascript>
<!--
function getFullHTML(doc)
{
var fullHTML;
var aIframeHTML = new Array(); for (var i=0; i<doc.frames.length; i++)
{
aIframeHTML.push(getFullHTML(doc.frames[i].document));
} fullHTML = doc.documentElement.outerHTML; if(aIframeHTML.length > 0)
{
var regIframe = /<\/iframe>/gi;
var ind = 0;
fullHTML = fullHTML.replace(regIframe, function($0) {
return aIframeHTML[ind++] + $0 + "\n\n";
});
} return fullHTML;
}document.getElementById("ttaOutput").value = getFullHTML(document);
//-->
</SCRIPT>
</BODY></HTML>
p1.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
</head> <body>
<h1>页面 1 包含 页面 2</h1>
<iframe id="iframe12" frameborder=0 scrolling=no src="p2.html"></iframe> <h1>页面 1 包含 页面 3</h1>
<iframe id="iframe13" frameborder=0 scrolling=no src="p3.html"></iframe>
<br />
<textarea id="ttaOutput" rows="20" cols="60"></textarea><input type="button" value="show" id="btnShow" /> <script type="text/javascript" defer>
<!--
function getFullHTML(win)
{
var fullHTML;
var aIframeHTML = new Array();
var cFrame = win.document.frames || win.frames; for (var i=0; i<cFrame.length; i++)
{
aIframeHTML.push(getFullHTML(cFrame[i]));
} // For IE only.
//fullHTML = win.document.documentElement.outerHTML;
fullHTML = "\n<HTML>\n" + win.document.documentElement.innerHTML + "\n</HTML>\n"; if(aIframeHTML.length > 0)
{
var regIframe = /<\/iframe>/gi;
var ind = 0;
fullHTML = fullHTML.replace(regIframe, function($0) {
// For IE only.
//return aIframeHTML[ind++] + $0 + "\n\n";
return "\n<HTML>\n" + aIframeHTML[ind++] + $0 + "\n</HTML>\n";
});
} return fullHTML;
}function $(sId) {
return document.getElementById(sId);
}$("btnShow").onclick = function() {
$("ttaOutput").value = getFullHTML(window);
};
//-->
</script>
</body>
</html>