...

View Full Version : Recursive call from external js



mrbaron
12-21-2005, 06:25 PM
I am simply trying to get a variable to loop up to 30 and dynamically update the value on screen.

I have this code in my external js file:

var i=0;
more_color();

function more_color(){

if(i <= 31){

document.writeln(i);
i_color++;
document.writeln(i);
var timer=window.setTimeout("more_color()",1000); }
else{
}
}

And this in my html:

<head>
<script src="testing2.js" type="text/javascript">
</script>

The output is:

0 1
after 1 second:
1 2
after 1 more second:
IE error - object expected line 1 char 1!!!!

Why is this happening?

liorean
12-21-2005, 06:33 PM
document.write and document.writeln only works while the document stream is open (the browser hasn't finished parsing the HTML yet). When the browser has closed the document stream, those methods will instead open a new document stream in place of the old one, and overwrite it.

dmitryseliv
12-21-2005, 07:07 PM
Try something like that.

<script type="text/javascript">

//CREATE LOOP
for (i = 0; i <= 30; i++)
{
document.write(i)

}

</script>

mrbaron
12-22-2005, 10:59 AM
document.write and document.writeln only works while the document stream is open (the browser hasn't finished parsing the HTML yet). When the browser has closed the document stream, those methods will instead open a new document stream in place of the old one, and overwrite it.

Thanks for that, but is there a commonly known workaround for this? My js knowledge is very limited.

liorean
12-22-2005, 01:15 PM
The most common workaround is changing the innerHTML property of an element. Another possibility is to generate the nodes using the DOM.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum