You are calling document.write after the page has loaded. When you do that, whatever you write will REPLACE the document. This is one reason why document.write should be avoided. And since it replaced your document, the browser is probably waiting for <html></html> so it knows to stop processing.
Instead of document.write, use DOM methods like appendChild and createNode or createTextNode.