Reputation: 2656
I need to replace accented characters in a string with their unaccented counterparts and I implemented this http://lehelk.com/2011/05/06/script-to-remove-diacritics/.
var defaultDiacriticsRemovalMap = [
{'base':'A', 'letters':/[\u0041\u24B6\uFF21\u00C0\u00C1\u00C2\u1EA6\u1EA4\u1EAA\u1EA8\u00C3\u0100\u0102\u1EB0\u1EAE\u1EB4\u1EB2\u0226\u01E0\u00C4\u01DE\u1EA2\u00C5\u01FA\u01CD\u0200\u0202\u1EA0\u1EAC\u1EB6\u1E00\u0104\u023A\u2C6F]/g},
/* ...
so on and so forth
.... */
];
var changes;
function removeDiacritics (str) {
if(!changes) {
changes = defaultDiacriticsRemovalMap;
}
for(var i=0; i<changes.length; i++) {
str = str.replace(changes[i].letters, changes[i].base);
}
return str.replace(/\s+/g, '_'); //space to dash
}
This fails in IE 7 and 8 (works in every other browser) with the error 'changes[...].letters' is null or not an object
on the line str = str.replace(changes[i].letters, changes[i].base);
, and I have no idea why. I mean, I understand that the browser thinks that it had encountered a null value, but I fail to see how can this be the case.
I copied my code to a jsfiddle (it fires the same error there as well) so you can take a look at it.
Upvotes: 0
Views: 1200
Reputation: 147363
Almost certainly you have a trailing comma in the array. IE treats it as an elision and adds one to the length, effectively adding an extra (undefined) member. Other browsers correctly ignore it. In any case, remove it.
Just to show how IE treats the comma:
var a = [0,1,];
alert(a.length); // 3 in IE, 2 in others
alert(a.hasOwnProperty('2')); // false in all browsers
The trailing comma isn't an elision, two trailing commas are required for that.
Upvotes: 2
Reputation: 12395
You have an extra comma (,) before your array's closing bracket (]), IE7/8 then add an extra undefined
value to this array, so when loop using for
iteration, IE7/8 would encounter a value of undefined
at index of 84 (try to log changes.length
and see difference)
Upvotes: 1
Reputation: 5602
I'm not sure that this is you problem as you don't have your whole code pasted, but I know for sure that IE7/8 throws an error if you add an extra comma after your last element of an array.
So if you have something like this:
var defaultDiacriticsRemovalMap = [
{'base':'A'},
{'k1':'v1'},
...
{'k_last':'v_last'}, // this last comma will break your js in IE7/8
];
Upvotes: 3