Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Dec 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Recursive call from external js

    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?

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    New to the CF scene
    Join Date
    Dec 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try something like that.

    <script type="text/javascript">

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

    }

    </script>

  • #4
    New to the CF scene
    Join Date
    Dec 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by liorean
    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.

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    The most common workaround is changing the innerHTML property of an element. Another possibility is to generate the nodes using the DOM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •