How to get the entire document HTML as a string?

前端 未结 15 1811
暖寄归人
暖寄归人 2020-11-22 17:00

Is there a way in JS to get the entire HTML within the html tags, as a string?

document.documentElement.??
相关标签:
15条回答
  • 2020-11-22 17:38

    You have to iterate through the document childNodes and getting the outerHTML content.

    in VBA it looks like this

    For Each e In document.ChildNodes
        Put ff, , e.outerHTML & vbCrLf
    Next e
    

    using this, allows you to get all elements of the web page including < !DOCTYPE > node if it exists

    0 讨论(0)
  • 2020-11-22 17:40
    document.documentElement.innerHTML
    
    0 讨论(0)
  • 2020-11-22 17:41

    MS added the outerHTML and innerHTML properties some time ago.

    According to MDN, outerHTML is supported in Firefox 11, Chrome 0.2, Internet Explorer 4.0, Opera 7, Safari 1.3, Android, Firefox Mobile 11, IE Mobile, Opera Mobile, and Safari Mobile. outerHTML is in the DOM Parsing and Serialization specification.

    See quirksmode for browser compatibility for what will work for you. All support innerHTML.

    var markup = document.documentElement.innerHTML;
    alert(markup);
    
    0 讨论(0)
  • 2020-11-22 17:41

    The correct way is actually:

    webBrowser1.DocumentText

    0 讨论(0)
  • 2020-11-22 17:45

    You can do

    new XMLSerializer().serializeToString(document)
    

    in browsers newer than IE 9

    See https://caniuse.com/#feat=xml-serializer

    0 讨论(0)
  • 2020-11-22 17:46

    You can also do:

    document.getElementsByTagName('html')[0].innerHTML
    

    You will not get the Doctype or html tag, but everything else...

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