So.... I made some sample code to show you the issue..... I just stumbled upon this problem the other day.. its not really a question just putting it on here so people can comment about it. Test the code and as you can see i'm not globally declaring a single variable. however when you put variables without slapping the word var infront of them they automatically become global... this includes for (i=0)... I don't believe this is how its supposed to function.
Code:
<script>
window.onload=function() {test();}
/*######################## iam_clint ########################
Please take note that I am not globally declaring anything
if you change the for loops to be for(var i=0; that actually resolves the problem.
######################################################*/
function test() {
test_global="This isn't supposed to be global.";
for (i=0; i<=10; i++) {
alert("test " + i);
test2();
}
if (i>10) { alert("i is greater than 10 (" + i + ")"); }
test3();
}
function test2() {
for (i=0; i<=10; i++) {
c=2;
}
}
function test3() {
alert(test_global);
}
</script>
Comment