The recommended standard reference from the node to a document has been
node.ownerDocument since DOM Level 2. According to MSDN: ownerDocument is supported since IE6. In IEs
node.document was also supported until IE10.
The fix for your code would hence be:
document.write was used in the example only to demonstrate the output, not as real code, hence I'm not handling its use in this answer.