How do I get the HTML source from the page?

前端 未结 5 457
谎友^
谎友^ 2020-11-29 07:37

Is there a way to access the page HTML source code using javascript?

I know that I can use document.body.innerHTML but it contains only the code inside

5条回答
  •  一整个雨季
    2020-11-29 08:03

    Provided that

    • true html source code is wanted (not current DOM serization)
    • and that the page was loaded using GET method,

    the page source can be re-downloaded:

    fetch(document.location.href)
        .then(response => response.text())
        .then(pageSource => /* ... */)
    

提交回复
热议问题