Javascript iframe problem

Does anyone know how to get HTML from IFRAME? I tried several different ways:

document.getElementById('iframe01').contentDocument.body.innerHTML, document.frames['iframe01'].document.body.innerHTML, document.getElementById('iframe01').contentWindow.document.body.innerHTMLEtc.

but none of them worked.

I believe that the reason why they do not work is because the contents of my iframe do not have the body tag (I am loading XML). Any other way to get all the contents of an iframe? I am also open to jQuery.

It:

document.getElementById('iframe01').contentWindow.document.body.innerHTML

works fine in IE, but this:

document.getElementById('iframe01').contentDocument.body.innerHTML

not working for ff.

+3
source share
2 answers

Since you checked this jQuery, I assume that you will appreciate the answer in jQuery to get around cross-browser issues.

$("#iframe01").contents()

jQuery iframe. . HTML- ( , , " htmldocument" ).

:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script>
$(function() {
    $("#clickme").click(function() {
      alert($("#iframe01").contents().find("body").html());
      return false;
    });     
});
</script>
</head>
<body>
  <p id="hello">Hello World, there an iframe below this. <a href="#" id="clickme">Click me!</a></p>
  <iframe id="iframe01" src="iframe_content.html"></iframe>
</body>
</html>

, iframe , iframe , - .

+2

. :

document.frames["iframe0"].contentWindow.document.body.innerHTML

IE, FF

0

Source: https://habr.com/ru/post/1730324/


All Articles