Skip to content

Instantly share code, notes, and snippets.

@jaygooby
Created March 23, 2012 12:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaygooby/2170045 to your computer and use it in GitHub Desktop.
Save jaygooby/2170045 to your computer and use it in GitHub Desktop.
Replacing text nodes http://stackoverflow.com/a/1175796/391826 patched for MSIE
function htmlreplace(a, b, element) {
if (!element) element = document.body;
var nodes = element.childNodes;
for (var n=0; n<nodes.length; n++) {
// MSIE doesn't have Node
if (nodes[n].nodeType == 3) {
var r = new RegExp(a, 'gi');
// MSIE8 and less doesn't have textContent
if (nodes[n].textContent) {
// If we assign try and assign a new value to
// nodes[n].textContent
// then 2011 bulds of MSIE9 crash with "A problem with this
// website caused Internet Explorer to close"
// (later builds are OK).
// To be safe, assign to nodes[n].nodeValue instead.
//nodes[n].textContent = nodes[n].textContent.replace(pattern, replacement);
nodes[n].nodeValue = nodes[n].textContent.replace(r, b);
} else {
nodes[n].nodeValue = nodes[n].nodeValue.replace(r,b);
}
} else {
htmlreplace(a, b, nodes[n]);
}
}
}
@elishaterada
Copy link

Thank you for taking an extra step to patch the script, I will be using this for my client websites.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment