Javascript Iframe innerHTML

后端 未结 10 2485
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 07:08

Does anyone know how to get the HTML out of an IFRAME I have tried several different ways:

document.getElementById(\'iframe01\').contentDocument.body.innerHT         


        
相关标签:
10条回答
  • 2020-11-27 07:58

    Conroy's answer was right. In the case you need only stuff from body tag, just use:

    $('#my_iframe').contents().find('body').html();
    
    0 讨论(0)
  • 2020-11-27 07:59

    If you take a look at JQuery, you can do something like:

    <iframe id="my_iframe" ...></iframe>
    
    $('#my_iframe').contents().find('html').html();
    

    This is assuming that your iframe parent and child reside on the same server, due to the Same Origin Policy in Javascript.

    0 讨论(0)
  • 2020-11-27 08:02

    You can use the contentDocument or contentWindow property for that purpose. Here is the sample code.

    function gethtml() {
      const x = document.getElementById("myframe")
      const y = x.contentWindow || x.contentDocument
      const z = y.document ? y.document : y
      alert(z.body.innerHTML)
    }
    

    here, myframe is the id of your iframe. Note: You can't extract the content out of an iframe from a src outside you domain.

    0 讨论(0)
  • 2020-11-27 08:02

    You can get the source from another domain if you install the ForceCORS filter on Firefox. When you turn on this filter, it will bypass the security feature in the browser and your script will work even if you try to read another webpage. For example, you could open FoxNews.com in an iframe and then read its source. The reason modern web brwosers deny this ability by default is because if the other domain includes a piece of JavaScript and you're reading that and displaying it on your page, it could contain malicious code and pose a security threat. So, whenever you're displaying data from another domain on your page, you must beware of this real threat and implement a way to filter out all JavaScript code from your text before you're going to display it. Remember, when a supposed piece of raw text contains some code enclosed within script tags, they won't show up when you display it on your page, nevertheless they will run! So, realize this is a threat.

    http://www-jo.se/f.pfleger/forcecors

    0 讨论(0)
提交回复
热议问题