Others have given you the answer about appendChild.
Calling document.write()
on a page that is not open (e.g. has finished loading) first calls document.open()
which clears the entire content of the document (including the script calling document.write), so it's rarely a good idea to do that.