Created
March 23, 2012 12:02
-
-
Save jaygooby/2170045 to your computer and use it in GitHub Desktop.
Replacing text nodes http://stackoverflow.com/a/1175796/391826 patched for MSIE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for taking an extra step to patch the script, I will be using this for my client websites.