简体   繁体   English

如何使用javascript或jquery从iframe获取完整的源代码?

[英]How to get full source code from an iframe with javascript or jquery?

I am helpless. 我很无奈。 I need to get the source code from an iframe with Javascript or Jquery. 我需要使用Javascript或Jquery从iframe获取源代码。

The source file of the iframe is in the same domain . iframe的源文件位于同一个域中 Here is my iframe: 这是我的iframe:

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

I am using the following so famous syntax to get the source code from the iframe: 我使用以下如此着名的语法来从iframe获取源代码:

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

But with this code, I am only able to get the code inside BODY element. 但是使用这段代码,我只能在BODY元素中获取代码。 I want to get full code from <!doctype html> to <html> . 我想从<!doctype html>到<html>获取完整的代码。

Please help me. 请帮我。

Use documentElement instead of body and outerHTML instead of innerHTML . 使用documentElement而不是bodyouterHTML而不是innerHTML

Example: 例:

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

Note that this will exclude the doctype and any comments that are not a child of the HTML node. 请注意,这将排除doctype和任何不是HTML节点的子项的注释。 If you need to include this data, you will need to make a separate AJAX request to fetch it. 如果需要包含此数据,则需要单独的AJAX请求来获取它。 The doctype could also be reconstructed from document.doctype . doctype也可以从document.doctype重建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM