document.write() is used only during the load of HTML documents. It does not work with XHTML.
document.write() does not work on already loaded documents.
Once you have a loaded document, you can use DOM methods to add or remove nodes to existing nodes. Nodes in an HTML DOM correspond to elements in the HTML markup. To work from a particular node, you should use an 'id' attribute in the markup and then use the DOM method (document.getElementById('id-attribute-value') to assign it to a JS variable, from which you can use methods or reference properties for a node of element type. Another type of node in the HTML DOM is a text node, and it has its own methods and properties. I suggest you get the PDF on the W3C HTML DOM Core and for HTML elements so you can understand what the methods and properties are.
Moreover, if you want to track the result of changes you make to by your use of the methods, I suggest you install & use Firefox and then install the add-on Firebug: this allows you to interact with the document and identify the HTML markup, CSS styling, aspects of DOM. "View Source" is rendered at load time, and probably does not incorporate dynamic changes to the document done by scripts.