11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

value that contains the value entered by the user in the prompt. <strong>The</strong> basic syntax is shown<br />

here:<br />

resultvalue = window.prompt(prompt string, default value string);<br />

<strong>The</strong> shorthand prompt() is almost always used instead of window.prompt() and occasionally<br />

programmers will use only a single value in the method.<br />

result = prompt("What is your favorite color?");<br />

However, in most browsers you should see that a value of undefined is placed in the prompt<br />

line. You should set the second parameter to an empty string to keep this from happening.<br />

result = prompt("What is your favorite color?","");<br />

It is important when using the prompt() method to understand what is returned. If the user<br />

clicks the Cancel button in the dialog or clicks the Close box, a value of null will be returned. It<br />

is always a good idea to check for this. Otherwise, a string value will be returned. Programmers<br />

should be careful to convert prompt values to the appropriate type using parseInt() or similar<br />

methods if they do not want a string value.<br />

<strong>The</strong> next example shows the prompt() method in action.<br />

<br />

<br />

<br />

Ask the <strong>JavaScript</strong> Guru 1.0<br />

<br />

<br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!