Aii*_*ias 9 javascript iframe jquery doctype dom
我试图找出如何检索完整(这意味着所有的数据)的HTML页面源来自<iframe>其src来自同一原始域作为它嵌入页面.我想在任何给定时间获得确切的源代码,由于Javascript或php生成<iframe>html输出,这可能是动态的.这意味着AJAX调用$.get()对我来说不起作用,因为页面可能已经通过Javascript修改或根据请求时间或mt_rand()在php中唯一生成.我无法<!DOCTYPE>从我的身上找到确切的声明<iframe>.
我一直在试验并搜索Stack Overflow并且没有找到一个解决方案来检索所有页面源代码,包括<!DOCTYPE>声明.
一位在回答如何获得与jQuery整个页面的HTML?建议,为了检索<!DOCTYPE>信息,你需要手动构造此声明,通过检索<iframe>的document.doctype属性,然后将所有的属性到<!DOCTYPE>申报自己.这真的是从<iframe>HTML页面源中检索此信息的唯一方法吗?
以下是我看过的一些值得注意的Stack Overflow帖子,这不是重复的:
下面是我的一些本地测试代码,说明到目前为止我最好的尝试,只检索内包括数据<iframe>的<html>标签:
main.html中
<html>
<head>
<title>Testing with iframe</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
function test() {
var doc = document.getElementById('iframe-source').contentWindow.document;
var html = $('html', doc).clone().wrap('<p>').parent().html();
$('#output').val(html);
}
</script>
</head>
<body>
<textarea id="output"></textarea>
<iframe id="iframe-source" src="iframe.html" onload="javascript:test()"></iframe>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
Iframe.html的
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html class="html-tag-class">
<head class="head-tag-class">
<title>iframe Testing</title>
</head>
<body class="body-tag-class">
<h2>Testing header tag</h2>
<p>This is <strong>very</strong> exciting</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
以下是在Google Chrome版本27.0.1453.110 m中一起运行的这些文件的屏幕截图:

正如你可以看到,谷歌Chrome的Inspect element表明内<iframe>的<!DOCTYPE>声明是存在的,所以我怎么能检索与该页面的源代码这一数据?此问题也适用于标记中未包含的任何其他声明或其他<html>标记.
任何有关通过Javascript检索此完整页面源代码的帮助或建议将不胜感激.
这是一种从 doctype 构建它的方法,似乎适用于 html 4 和 5,我没有测试像 svg 这样的东西。
<html>
<head>
<title>Testing with iframe</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
function test() {
var d = document.getElementById('iframe-source').contentWindow.document;
var t = d.docType;
$('#output').val(
"<!DOCTYPE "+t.name+
(t.publicId? (" PUBLIC "+JSON.stringify(t.publicId)+" ") : "")+
(t.systemId? JSON.stringify(t.systemId) :"")+
">\n" + d.documentElement.outerHTML );
}
</script>
</head>
<body>
<textarea id="output"></textarea>
<iframe id="iframe-source" src="iframe.html" onload="test()"></iframe>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这还使用 HTML.outerHTML 来确保您获得 documentElement 上的任何属性。
| 归档时间: |
|
| 查看次数: |
3145 次 |
| 最近记录: |