'textContent' is null or not an object-Collection of common programming errors
i have a problem with this code this code run great on all browsers exept IE,my IE is 8.0 any solution? i won’t using jquery sincerely Note: i changed Node.TEXT_NODE to 3 but other error occured:’textContent’ is null or not an object
function replaceText(oldText, newText, node){
node = node || document.body;
var childs = node.childNodes, i = 0;
while(node = childs[i]){
if (node.nodeType == Node.TEXT_NODE){
node.textContent = node.textContent.replace(oldText, newText);
} else {
replaceText(oldText, newText, node);
}
i++;
}
}
old
old Click me! whatever
i found it
while(i < childs.length){
if (rgx.test(document.body.innerHTML)){
childs[i][textPropName] =childs[i][textPropName].replace(rgx,'new');
}
else
{
replaceText(oldText, newText,document.body.childNodes[i])
}
i++;
}
}
-
Node.TEXT_NODE
&textContent
are not available in IE8.Use
3
instead ofNode.TEXT_NODE
, and useinnerText
iftextContent
is not available:var textPropName = node.textContent === undefined ? 'innerText' : 'textContent'; if (node.nodeType == 3) { node[textPropName] = node[textPropName].replace(oldText, newText); } else { replaceText(oldText, newText, node); }
You should probably cache that
textPropName
outside your function, so that you don’t recheck it every single time the function is called (usedocument.body
for the test).
Originally posted 2013-11-09 20:02:56.