I wrote a simple 'replaceAll' function that extends String.prototype.
String.prototype.replaceAll = function (removalChar, insertionChar) {
var output = "";
for (var i = 0; i < this.length; i++) {
if(this[i] == removalChar) {
output += insertionChar;
}
else {
output += this[i];
}
}
return output;
}
Test code:
var test = "Hello-1-2-3";
alert(test.replaceAll("-"," "));
My test code alerts Hello 1 2 3
in all browsers including IE9.
But in IE7 and 8, the output I get is something like this: undefinedundefinedundefinedundefinedundefinedundefined...
jsFiddle: http://jsfiddle.net/cd4Z2/ (try this in IE7/IE8)
How could I possibly rewrite the function to ensure it works on IE7/8 without breaking its behaviour on other browsers?