D
dd
Hi,
I've discovered a scenario where Safari 1.3 (I need to make my stuff
compliant with 1.3+) gets confused about the scope of local variables
WITHIN functions that were created in dynamic script blocks. I've made
this example where function def has a local i variable in a loop, and
it calls function abc which also has a local i variable in a loop. What
happens is that Safari is not respecting the scope and is allowing the
called function to corrupt a local variable in the parent function
Here's the whole test page including html tags. If you try it you'll
see that IE and Gecko both produce the output "in abc" twice, because
the def function correctly gets to call abc twice. On Safari, i gets
corrupted, and abc only gets called once... Any ideas what I can do to
prevent this?
<html><body>
<script language="javascript">
function abc() {
for(var i=0; i<1; i++)
document.getElementById("dd").innerHTML+="in abc<br>";
}
var funcs=("\n" + abc.toString());
function def() {
for(var i=0; i<2; i++)
abc();
}
funcs += ("\n" + def.toString());
//I now have the funcs abc and def in string form that I'll put into my
dynamic script element
var s=document.createElement('SCRIPT');
if(navigator.userAgent.toLowerCase().indexOf("safari")!=-1)
s.innerHTML=funcs; //safari needs this way
else
s.text=funcs; //normal browsers need this
var b=document.getElementsByTagName('body')[0];
if(b)b.appendChild(s); //create those two funcs now
setTimeout(def,1000);
</script>
<div id='dd'></div>
</body></html>
I've discovered a scenario where Safari 1.3 (I need to make my stuff
compliant with 1.3+) gets confused about the scope of local variables
WITHIN functions that were created in dynamic script blocks. I've made
this example where function def has a local i variable in a loop, and
it calls function abc which also has a local i variable in a loop. What
happens is that Safari is not respecting the scope and is allowing the
called function to corrupt a local variable in the parent function
Here's the whole test page including html tags. If you try it you'll
see that IE and Gecko both produce the output "in abc" twice, because
the def function correctly gets to call abc twice. On Safari, i gets
corrupted, and abc only gets called once... Any ideas what I can do to
prevent this?
<html><body>
<script language="javascript">
function abc() {
for(var i=0; i<1; i++)
document.getElementById("dd").innerHTML+="in abc<br>";
}
var funcs=("\n" + abc.toString());
function def() {
for(var i=0; i<2; i++)
abc();
}
funcs += ("\n" + def.toString());
//I now have the funcs abc and def in string form that I'll put into my
dynamic script element
var s=document.createElement('SCRIPT');
if(navigator.userAgent.toLowerCase().indexOf("safari")!=-1)
s.innerHTML=funcs; //safari needs this way
else
s.text=funcs; //normal browsers need this
var b=document.getElementsByTagName('body')[0];
if(b)b.appendChild(s); //create those two funcs now
setTimeout(def,1000);
</script>
<div id='dd'></div>
</body></html>