Javascript Iframe innerHTML

后端 未结 10 2592
没有蜡笔的小新
没有蜡笔的小新 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 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.

提交回复
热议问题