I faced this strange situation where foreach like construct of javascript does not work in IE but it works in FF. Well not all for..in
just this special funciton does not work. I will post the code. Tested in IE8. Tested also with XHTML DTD.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> Test </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<script type="text/javascript">
<!--
String.prototype.format = function() {
var formatted = this;
var mycars = new Array(); //some
mycars[0] = "Saab";
mycars[1] = "Volvo";
mycars[2] = "BMW";
var arg;
for (arg in mycars) { alert('it comes here');
formatted = formatted.replace("{" + arg + "}", arguments[arg]); }
return formatted;
};
String.prototype.format2 = function() {
var formatted = this;
var arg;
for (arg in arguments) { alert('it does not come here');
formatted = formatted.replace("{" + arg + "}", arguments[arg]); }
return formatted;
};
function fn() {
var s = 'The {0} is dead. Don\'t code {0}. Code {1} that is open source!'.format('ASP', 'PHP');
alert('format:'+s);
var s2 = 'The {0} is dead. Don\'t code {0}. Code {1} that is open source!'.format2('ASP', 'PHP');
alert('format2:'+s2); //does not replace {0}s and {1}s
}
//-->
</script>
<BODY>
<input type="button" value="click " onclick="fn();" />
</BODY>
</HTML>
Update I posted a wrong question that it works in FireFox but not in IE8 which was wrong. It does not work in FireFox too. Actually I got this code from post JavaScript equivalent to printf/string.format.