Javascript Undefined Message

Discussion in 'JavaScript' started by johnpmc22, Aug 22, 2007.

  1. #1
    Hi All,
    I am in the process of putting together a weg page. Altough all the information defined in my function appears (in table) under the table it says undefined??? just under the table caption

    Please see code below, any ideas on how to get rid of this error?

    Any ideas????:):)

    document.write(test());

    function test()
    {
    document.write('<center><table width="50%" border="1" bordercolor="0000CC">');
    document.write('<th align="center">Test</th>');
    document.write('</table></center>');
    document.write('<center><table width="100%" border="1" bordercolor="0000CC">');

    document.write('<th align="center">Name</th><th align="center">Surname</th><th align="center">Age</th>');

    var Name = "John";
    var Type = "Mc";
    var Version = "5";

    document.write('<tr align="center"><td>' + Name + '</td><td>' + Type + '</td><td>' + Version + '</td</tr>');


    document.write('</table></center>');
    document.write('<center><caption>Table: Test</caption></center>');
    }
     
    johnpmc22, Aug 22, 2007 IP
  2. lbalance

    lbalance Peon

    Messages:
    381
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    it might be parsing document.write(test());
    before it sees the functions, so put that after
    you declare the function
     
    lbalance, Aug 22, 2007 IP
  3. johnpmc22

    johnpmc22 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I Also meant to say that even if i take all the information out of the function :

    eg

    document.write(test());

    function test()
    {
    }

    I still get undefined error under the heading and then next table with the information and under captain 'undefined':)
     
    johnpmc22, Aug 22, 2007 IP
  4. lbalance

    lbalance Peon

    Messages:
    381
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #4
    try it like this:

    function test()
    {
    }

    document.write(test());



    the browser needs to load the function first
     
    lbalance, Aug 22, 2007 IP
  5. johnpmc22

    johnpmc22 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks IBalance for the quick reply, i tried what you suggested

    function test()
    {}
    document.write(test());

    Sorry no Joy, still the information is in the table and after the captain 'undefined' is still sitting there

    but thanks again
     
    johnpmc22, Aug 22, 2007 IP
  6. lbalance

    lbalance Peon

    Messages:
    381
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #6
    ahh, cant document.write a function

    try something like this:

    <script type="text/javascript">
    function test()
    {
    document.write('hello');
    }
    test();
    </script>
     
    lbalance, Aug 22, 2007 IP
  7. Jamie18

    Jamie18 Peon

    Messages:
    201
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    i'm not sure on this but could it have something to do with trying to pass a function filled with document.write()'s to the document.write()?

    what if you set it up like this?


    function test()
    {
    ...
    }
    bodyonload = test();
     
    Jamie18, Aug 22, 2007 IP
  8. johnpmc22

    johnpmc22 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I should have expalained that i was using a external js script so i couldn't use javascript tags.

    The solution that Jamie18 suggested was 3 quaters there,

    Firstly i was calling the js script for the head of my html page, i moved this to the body to come in line with Jamie18's suggestion to use bodyonload = test(); instead of document.write(test());

    But when i placed bodyonload = test(); after the function no information appeared so i placed it before the function like so:

    bodyonload = test();
    function test()
    {}

    And this worked great

    Thanks for you help Ibalance and Jamie18.:):):)
     
    johnpmc22, Aug 22, 2007 IP