【问题标题】:How to get full source code from an iframe with javascript or jquery?如何使用 javascript 或 jquery 从 iframe 获取完整的源代码?
【发布时间】:2014-09-05 03:50:20
【问题描述】:

我很无奈。我需要使用 Javascript 或 Jquery 从 iframe 获取源代码。

iframe 的源文件在同一个域中。这是我的 iframe:

<iframe src="/source_file.html" id="iframe_id"></iframe>

我正在使用以下如此著名的语法从 iframe 中获取源代码:

document.getElementById("iframe_id").contentWindow.document.body.innerHTML

但是使用这段代码,我只能在 BODY 元素中获取代码。我想获取从 的完整代码。

请帮帮我。

【问题讨论】:

  • 尝试$('html', document.getElementById("iframe_id").contentWindow.document)[0].outerHTML 仍然不会给出文档类型

标签: javascript jquery html iframe


【解决方案1】:

使用documentElement 代替bodyouterHTML 代替innerHTML

示例:

document.getElementById("iframe_id").contentWindow.document.documentElement.outerHTML

请注意,这将排除 doctype 和任何不是 HTML 节点子节点的 cmets。如果您需要包含此数据,则需要发出单独的 AJAX 请求来获取它。文档类型也可以从document.doctype 重构。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多