By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,035 Members | 944 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,035 IT Pros & Developers. It's quick & easy.

writing information from a prompt

P: 6
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..
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <SCRIPT LANGUAGE="JavaScript">
  4. function getName() {
  5. var name = prompt("What is your name?", "")
  6. }
  7. document.write("Hello " + name + " welcome to my site");
  8. </SCRIPT>
  9.  
  10.  
  11. </head>
  12. <body>
  13. <BODY onLoad="getName()">
  14. <SCRIPT LANGUAGE="JavaScript">
  15. document.write("Hello " + name + " welcome to my site");
  16. </SCRIPT>
  17. </body>
  18. </html>
Jan 20 '10 #1
Share this Question
Share on Google+
2 Replies


P: 86
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:

Expand|Select|Wrap|Line Numbers
  1. function getName() {
  2. var name = prompt("What is your name?", "")
  3. document.body.innerHTML+="Your name is "+name;
  4. }
  5.  
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.getElementById.
Jan 20 '10 #2

P: 6
Thank you so much larztheloser
Jan 20 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.