How to get the entire document HTML as a string?

前端 未结 15 1816
暖寄归人
暖寄归人 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:57

    I always use

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

    Probably not the right way but I can understand it when I see it.

    0 讨论(0)
  • 2020-11-22 18:01

    PROBABLY ONLY IE:

    >     webBrowser1.DocumentText
    

    for FF up from 1.0:

    //serialize current DOM-Tree incl. changes/edits to ss-variable
    var ns = new XMLSerializer();
    var ss= ns.serializeToString(document);
    alert(ss.substr(0,300));
    

    may work in FF. (Shows up the VERY FIRST 300 characters from the VERY beginning of source-text, mostly doctype-defs.)

    BUT be aware, that the normal "Save As"-Dialog of FF MIGHT NOT save the current state of the page, rather the originallly loaded X/h/tml-source-text !! (a POST-up of ss to some temp-file and redirect to that might deliver a saveable source-text WITH the changes/edits prior made to it.)

    Although FF surprises by good recovery on "back" and a NICE inclusion of states/values on "Save (as) ..." for input-like FIELDS, textarea etc. , not on elements in contenteditable/ designMode...

    If NOT a xhtml- resp. xml-file (mime-type, NOT just filename-extension!), one may use document.open/write/close to SET the appr. content to the source-layer, that will be saved on user's save-dialog from the File/Save menue of FF. see: http://www.w3.org/MarkUp/2004/xhtml-faq#docwrite resp.

    https://developer.mozilla.org/en-US/docs/Web/API/document.write

    Neutral to questions of X(ht)ML, try a "view-source:http://..." as the value of the src-attrib of an (script-made!?) iframe, - to access an iframes-document in FF:

    <iframe-elementnode>.contentDocument, see google "mdn contentDocument" for appr. members, like 'textContent' for instance. 'Got that years ago and no like to crawl for it. If still of urgent need, mention this, that I got to dive in ...

    0 讨论(0)
  • 2020-11-22 18:02

    Use document.documentElement.

    Same Question answered here: https://stackoverflow.com/a/7289396/2164160

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