I have the following function (worked in IE6 but is broken in IE8)
function implode() { var str = '';
for(item in globvars) //<- IE8 wets itself here ...
str+= '\n' + globvars[item]+';';
return str+'\n';
}
It seems an innocuous little function, but IE8 dosent grok it. Can anyone show me how to rewrite this so it work in IE8 (as well as the other browsers)?
[Edit]
At the begining of the script (i.e. first line after the tag, I have defined the globvars like this:
var globvars = new Array(); // This should give globvars global scope
The error from IE8 is:
Object does not support this action
You will, however, have an element with
id="item"orname="item". IE mirrors elements with an id/name not just asdocument.itembut alsowindow.item. (Obviously it’s bad practice to rely on either.)So when you say
item=without telling it you want avar, silly IE thinks you’re trying to assign to the existing HTMLElement with that id/name sitting in thewindow, and throws a fit because elements aren’t writable. This is one reason to always usevareven for global variables.You shouldn’t use
for...into iterate an Array. It doesn’t do what you think. It may return the array items in the wrong order, and potentially unexpected non-numeric properties. Always use the plain oldfor (var i= 0; i<array.length; i++)loop for getting the items in an Array;for...inis only forObjectused as a mapping.Anyhow, JavaScript’s built-in
join()almost does the work for you: