最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is there a way to set the entire content of a document? - Stack Overflow

programmeradmin5浏览0评论

I'm writing some unit tests with jest. I've captured the rendered HTML of some web pages in files and want to use those as data for my tests. I can load up the <body> of the saved HTML with:

const fs = require('node:fs');
html = fs.readFileSync('blah-blah-blah.body.html', 'utf8');
document.body.innerHTML = html;

which works, but it's annoying because while it's easy to get the full document with curl url -o blah-blah-blah.html, it's more complicated to post-process that to pull out just the <body> element. I tried the obvious thing:

html = fs.readFileSync('blah-blah-blah.document.html', 'utf8');
document.documentElement = html;

but apparently document.documentElement is read-only, so sadness.

I'm writing some unit tests with jest. I've captured the rendered HTML of some web pages in files and want to use those as data for my tests. I can load up the <body> of the saved HTML with:

const fs = require('node:fs');
html = fs.readFileSync('blah-blah-blah.body.html', 'utf8');
document.body.innerHTML = html;

which works, but it's annoying because while it's easy to get the full document with curl url -o blah-blah-blah.html, it's more complicated to post-process that to pull out just the <body> element. I tried the obvious thing:

html = fs.readFileSync('blah-blah-blah.document.html', 'utf8');
document.documentElement = html;

but apparently document.documentElement is read-only, so sadness.

Share Improve this question asked yesterday Roy SmithRoy Smith 2,1654 gold badges20 silver badges29 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 0

innerHTML property exists on all Elements. And document.documentElement returns an element. So you can just do the below

document.documentElement.innerHTML = '<html><head><title>TESTING</title></head><p>HELLO</p></html>'

Run this in your dev console and see this page disappear.

发布评论

评论列表(0)

  1. 暂无评论