writing information from a prompt

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bboyson
    New Member
    • Jan 2010
    • 6

    writing information from a prompt

    I am trying to get the code to write the users name onto the site.

    I think i need to call the functon before the page fully loads can someone tell me how to do this or how to fix my problem thanks..
    Code:
    <html>
    <head>
    <SCRIPT LANGUAGE="JavaScript">
    function getName() {
    var name = prompt("What is your name?", "")
    }
    document.write("Hello " + name + " welcome to my site");
    </SCRIPT>
    
    
    </head>
    <body>
    <BODY onLoad="getName()">
    <SCRIPT LANGUAGE="JavaScript">
    document.write("Hello " + name + " welcome to my site");
    </SCRIPT>
    </body>
    </html>
  • larztheloser
    New Member
    • Jan 2010
    • 86

    #2
    Hey bboyson,

    Your mistake is using document.write in the header. You don't want to write text to the header because text in the header is not visible.

    Rather, try putting the same script in the start of the body.

    Then you try to use document.write in a script that is accessing a variable which might not have loaded yet. Therefore, don't make it go onload, but rather before. Or, make it go onload, but don't write it until the variable is found - for example:

    Code:
    function getName() {
    var name = prompt("What is your name?", "")
    document.body.innerHTML+="Your name is "+name;
    }
    And delete your other two document.writes .

    If you wanted to, rather than writing to document.body, you could write to a layer using document.getEle mentById.

    Comment

    • bboyson
      New Member
      • Jan 2010
      • 6

      #3
      Thanks

      Thank you so much larztheloser

      Comment

      Working...