I don't think I can use insertBefore() because I do not know what element will be first in the DOM.
I am trying to insert a body tag (i.e. <body>).
Without using a library, try this:
document.body.insertBefore(document.createElement("div"), document.body.firstChild);
Well, but you can always check this:
If document body has at least one child, use document.body.insertBefore([the new element], document.body.firstChild)
If document body has no children, just use document.body.appendChild([the new element]).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With