问题
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