<!DOCTYPE html>
<script>
setTimeout(function(){
var serializer = new XMLSerializer();
alert(serializer.serializeToString(document.documentElement));
document.open();
alert(serializer.serializeToString(document.documentElement || document.createElement('undefined')));
document.write('<p>Тест</p>');
alert(serializer.serializeToString(document.documentElement));
}, 100);
</script>