tags:

views:

114

answers:

5

When visitors visit my page, I want them to prompted with a box, ask their name, and use that name in text. I created a script like this:

function askname() {
var x=prompt("What is your name?","");
}

I placed that script between the head tags. Then, I write <body onload="askname()">. The box prompted successfully. Now, I want to use that 'x' in text. First, I just wrote

<script type="text/javascript">document.write(x)</script>

But it does not work at all! Is there another method to do this? I plan to not using any javascript library, though. So I beg your help. Sorry if this is kind of stupid question, I'm newbie in JS. And sorry for my bad english, too...

A: 

var x is local to the askname function. you want document.write(prompt("what is your name?", ""))

just somebody
+4  A: 

It's bad practice to use document.write, besides if you invoke it after the document has loaded it will overwrite it. innerHTML would be suitable here since this is a relatively simple task, if you get into more complex things you should use DOM methods, create elements and populate the nodeValue property.

It's also a bad practice to use inline event handlers and inline JS, you should separate the two, I would place this in an external script so it's actually cached, nevermind my code sample since it's just a sample but that's how it should be done on a real page.

<body>
<div id="message"></div>
<script>
window.onload = function() {
    var name = prompt('What is your name?', '');
    if ( name ) {
        document.getElementById('message').innerHTML = name;
    }

}
</script>
</body>
meder
beat me too it! +1. Although Jim could put that code inside his askname() function, no? (I'm pretty new to JS too :-)
Tom
Thanks meder, already tried that and worked perfectly! Once again, thanks!
Jim
+1  A: 

There are two problems with your script. One is that the x variable is local to the function, so that when you exit the function the string is gone. The other problem is that the document.write is executed before the function, so even if the variable worked as intended, it would not be assigned yet.

Instead of a script writing out the name, put an element with an identifier:

<span id="name"></span>

In the function, you put the name in the element after getting it from the user:

function askname() {
   var x = prompt("What is your name?","");
   document.getElementById('name').innerHTML = x;
}
Guffa
+1  A: 

In addition to meder and Guffa's problems, there is yet one more!

Namely: prompt doesn't work any more in IE7+. For security reasons, according to Microsoft, but they're pretty much totally spurious. Either way, window.prompt is effectively dead. If you want to accept input from the user you will have to make your own on-page form and attach a click handler to the submission to take the value of name.

There are a number of libraries that provide a way to do this that looks a bit like prompt, but they can never be a drop-in replacement because the synchronous nature of prompt cannot be reproduced in event-driven JavaScript.

bobince
Hm, never knew this.
meder
A: 

Instead of a prompt, I'd suggest doing something a bit more DOM like this:

<input type="text" id="textinput">

<input type="button" value="OK" onClick="document.getElementById('txtarea').innerHTML=document.getElementById('textinput').value;">

<span id="txtarea"></span>

You could also use a <div> around the input elements and when you click the button have that <div> disappear with document.getElementById('inputdiv').style.display = 'none';

Good Luck!!!!!!

Carlson Technology