I have a code that doesn’t only work in IE. everything works fine on other browsers, the error that occurs in IE is this
Line: 11
Error: Function expected
this is the fiddle, you can copy paste it as you wish, I really don’t know what do. I have no idea why it doesn’t work at all
http://jsfiddle.net/laupkram/TDWd6/
Code:
<form name="formx">
<input type="text" name="txtMultiplier">
<input type="button" value="LOOP!" onClick="loop()">
</form>
<script>
function loop(){
var mynumbers = [0,1,2,3,4,5,6,7,8,9,10];
var num = parseInt(document.formx.txtMultiplier.value);
document.write("Simulating For Loop<br>");
for(var i = 0; i < mynumbers.length; i++){
var prod = num * mynumbers[i];
document.write(mynumbers[i].toString() + " x " + num.toString() + "=" + (prod).toString() + "<br>");
}
document.write("<br>");
document.write("Simulating Do While<br>");
var i = 0;
do{
var prod = num * mynumbers[i];
document.write(mynumbers[i].toString() + " x " + num.toString() + "=" + (prod).toString() + "<br>");
i++;
}while(i < mynumbers.length);
document.write("<br>");
document.write("Simulating While<br>");
var i = 0;
while(i < mynumbers.length){
var prod = num * mynumbers[i];
document.write(mynumbers[i].toString() + " x " + num.toString() + "=" + (prod).toString() + "<br>");
i++;
}
}
</script>
Not sure why, butAccording to this demo, loop is not a function when clicked. It is a property ofloopseams to be like some magic name.<input type="button"/>element which calls a loop function. Inonclickthispoints to an element itself and loop is a property of it equal to 1. That is why it fails with error. Possible fix: changeonclick="loop()"toonclick="window.loop()"For instance, here it starts working in IE, but
document.writedestroy all previouse DOM/JS and it stops execution after firstdocument.writeexecution.It will be better if you will use something like on demo below (results are stored in temporary variable which is next passed to
innerHTMLofresdiv):http://jsfiddle.net/TDWd6/5/
Also, for some reason, even this code does not work in IE when function name is
loop(in code and demo above it is calledloop1). See demo with code like above, but with function calledloop: http://jsfiddle.net/TDWd6/5/