As the other answers mentioned, your particular errors come from the fact that your node object is actually null. The most bullet-proof way of testing if node.parentNode exists and is not null is:
if ((typeof node==='undefined') || !node || !node.parentNode) return null;
This covers the following cases:
- the
node variable doesn't exist
- the
node variable is null or undefined
parentNode is falsy (undefined, null, false, 0, NaN, or '')
As per Blue Skies' comments, you should take care about the first check (typeof node === 'undefined') because it masks undeclared variables which may lead to problems later on:
function f() {
if (typeof node==='undefined') {
node = {}; // global variable node, usually not what you want
}
}