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
    Regular Coder
    Join Date
    May 2011
    Location
    new york
    Posts
    118
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Javascript outputting "undefined" in the html

    Code:
    <script type="text/javascript">
    function loadText()
    {
    var t1=setTimeout("document.getElementById('data').innerHTML='<center><img style=\"margin-top:30px;\" src=\"loading.gif\"  /></center>'",0000);
    var t2=setTimeout("document.getElementById('data').innerHTML='THEPAGE'",6000)
    }
    </script>
    The above is in the header, and I'm calling it in the html.

    Code:
    <script type="text/javascript">
    document.write(loadText());
    </script>
    in the page it displays properly, but then it also says "undefined" not sure why? anyone have any ideas?
    Last edited by phpchick; 03-14-2012 at 05:15 PM.

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,077
    Thanks
    38
    Thanked 498 Times in 492 Posts
    The script in the <head> section is probably being executed BEFORE the 'data' element in the <body> section is available.
    Try moving the <script> in the <head> to just before the last </body> tag.

    Plus, I'm not sure that is a valid function to execute in the setTimeout command (???)

  • #3
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    You don't need the document.write statement, which is printing the function's return value.

  • Users who have thanked Logic Ali for this post:

    phpchick (03-14-2012)

  • #4
    Regular Coder
    Join Date
    May 2011
    Location
    new york
    Posts
    118
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Logic Ali View Post
    You don't need the document.write statement, which is printing the function's return value.
    correct.

    I just did

    loadText() by itself and the undefined has now disappeared. Thank you.

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,633
    Thanks
    0
    Thanked 649 Times in 639 Posts
    setTimeout("document.getElementById('data').innerHTML='THEPAGE'",6000)

    should read:

    setTimeout(function() {document.getElementById('data').innerHTML='THEPAGE'},6000)

    setTimeout wants a function - NOT as string of text. The only reason it is currently working is because it uses an implicit eval() to convert what you have into the function the call expects.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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