you CAN NOT use document.write AFTER the page loads.
Do a view source on the page. You will see that document.write REPLACES the ENTIRE document.
If you want to switch, you need to either have the content on the page already, you need to just show it with css [display: block/none] or you need to use DOM methods [createElement/appendChild/removeChild] OR you use innerHTML.
subject: Problrm in calling userdefined method in onclick event of button dynamically