You're destroying the DOM with your document.write call. In some browsers, this also destroys global variables.
Instead use:
var element = document.createElement('h1');
element.appendChild(document.createTextNode('text'));
document.body.appendChild(element);