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

為{幸葍}努か 提交于 2020-08-05 04:14:40

问题


I am helpless. I need to get the source code from an iframe with Javascript or Jquery.

The source file of the iframe is in the same domain. Here is my 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:

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

But with this code, I am only able to get the code inside BODY element. I want to get full code from <!doctype html> to <html>.

Please help me.


回答1:


Use documentElement instead of body and outerHTML instead of 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. If you need to include this data, you will need to make a separate AJAX request to fetch it. The doctype could also be reconstructed from document.doctype.



来源:https://stackoverflow.com/questions/25678366/how-to-get-full-source-code-from-an-iframe-with-javascript-or-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!