Just curious, why do you need to test element.ownerDocument.defaultView.HTMLBodyElement? Would instanceof HTMLBodyElement not work? What about tagName == "BODY"?