The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Prompt Box Question

Hey Guys,

I am writing a tutorial for Spoono and I am running into a problem. This is a javascript tutorial that asks for input from a user through a prompt box, which is stored in the variable name. Then, there is a text box and submit button; when the user clicks on the Submit button, the variable name shows up in the text box. The script I have right now produces nothing in the text box. Here it is:

There are number of things wrong with your script, virtually all of them having to do with insufficient understanding of syntax and programming languages in general.

1) Do not use "type='submit'"
You are not submitting anything anywhere. (I don't care what any book or person tells you.) By using 'onclick=' you are asking the user's computer to process a command -- not the server (which 'type=submit' assumes you are doing)
2) you do not need to 'return false' here -- use that when you are dealing with hrefs, not buttons.
3)the DOM (document object model) begins with 'document', not the formname
4) never, ever give a variable a name that can be mistaken for something else -- 'name' -- If you looked at your error message in NN4 it said something like "window name has illegal character '/40'" or somesuch.
When the browser saw this: "....value=name" It tried to put the window.name in the text box. Since your window didn't have a name and the value the browser was looking at had some character that it wouldn't allow in a window.name, it just refused to do what you asked.

1) don't call it input() (see rule 4 above) -- Even if it works, don't; it may freak on the next round of browsers. Call it 'input1()' or 'promptUser()' or better yet, 'getName()' -- since that is what you are doing.
2) using the 'var' keyword within a function makes that variable local (in scope). That means that only the function recognizes and can use it. So, you either declare the variable outside of the function (really a lazy man's way of doing things) or remove the 'var' keyword so that it is global, or you can pass the result back to the calling function -- the best way because then the variable doesn't interfere with anything else on the page; however, in this case (because you are calling the prompt from the onload event handler), it would be extremely difficult to do and still have local scope.