简体   繁体   中英

Is there any way to append a Node after HTML closing tag </html>?

I need to append a node after html closing tag </html>
I tried a lot of document functions but nothing works else createComment()

document.appendChild(document.createComment('Comment'));

anything else show this error message:

Failed to execute 'appendChild' on 'Node': Only one element on document allowed

Note: I tried to work around this by using removeChild() to remove HTML tag with its full contents and then used document.write() to insert a new <html>...</html> with a new tag appended to it, and it works fine but the browser sanitized it and my new tag moved inside <body> .

So, Is there any way to append a Node after HTML closing tag </html> ?

From the official html reference , the <html> element:

  • represents the root of an HTML document .
  • contains a head element followed by a body element

This is its definition, shortly.
That means that no node can exist outside of it.

The only possible way to add that is by inspecting the entire page, and manually adding it, but the attempt will be short lived, because the input will get sanitized by the browser.

A validator would throw a Stray start tag type of error.
You can play around here to check what is a correct, accepted syntax and what not: https://html5.validator.nu/

And since you tagged the question , it's definitely not possible in js.
JS represents the parent element of the html element as null . If it wouldn't, maybe you had a chance, but that is a big if

document.documentElement.parentElement === null

So, in short, no , it is not possible

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM