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
<!DOCTYPE html>
<html>
<head>
<script>
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++;
}
}
</script>
</head>
<body>
old
<h1 id="myHeader" onclick="replaceText('old','new')">old Click me! whatever</h1>
</body>
</html>
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++;
}
}
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
<!DOCTYPE html>
<html>
<head>
<script>
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++;
}
}
</script>
</head>
<body>
old
<h1 id="myHeader" onclick="replaceText('old','new')">old Click me! whatever</h1>
</body>
</html>
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++;
}
}
Share
Improve this question
edited Jan 27, 2013 at 10:43
MD66
asked Jan 27, 2013 at 4:14
MD66MD66
1013 silver badges11 bronze badges
2
- @HovercraftFullOfEels - I've corrected that. – Stephen C Commented Jan 27, 2013 at 4:25
- i'm sory i use space for java script then forget to correct it after edit – MD66 Commented Jan 27, 2013 at 4:28
2 Answers
Reset to default 3Node.TEXT_NODE
& textContent
are not available in IE8.
Use 3
instead of Node.TEXT_NODE
, and use innerText
if textContent
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 (use document.body
for the test).
instead of textContent, use nodeValue
node.nodeValue = node.nodeValue.replace(oldText, newText);