The user of a web page can provide information either in response to prompts or as entries in forms on the web page. JavaScript can handle either mode of communication, and prompts can be used to guide the user to provide information in forms.
xstr is a String that will be displayed in a new window until the "OK" button in that window is clicked. The value returned is undefined
xstr is a String that will be displayed in a new window as a prompt requesting text input. The default text input is given by the String pstr. When the user types in that input and clicks the "OK" button in that window, the resulting String is returned as the value of the function.
document.write(xstr) writes the String xstr into the browser window as part of the HTML text.
document.FormName.TextAreaName.value = xstr loads the designed text area with the String xstr.
<form name="my_form"> <textarea name="my_text" rows=1 cols=90> </textarea> </form> <P> <script type="text/javascript" language="javascript"> <!-- This is the beginning of an HTML comment // This is a JavaScript comment alert("Welcome to this test page\nClick OK when you are ready"); tempF = prompt("Please tell me the current temperature in °F: ","68"); tempF = parseFloat(tempF); //convert the Srting to a number tempK = ((tempF - 32)*5)/9 + 273.15; //convert to degrees Kelvin tempK = (Math.round(tempK*100))/100; //round to the nearest 1/100th document.write("The temperature " + tempF + "°F is " + tempK + "°K <P>"); document.my_form.my_text.value = tempF + " degrees Fahrenheit is " + tempK + " degrees Kelvin"; // and end the comment --> </script> <noscript> <h3 style="text-align:center" align="center"> Your browser does not support JavaScript </h3> </noscript>
Prepared by Herbert J. Bernstein
27 October 2004.
© 2004 Herbert J. Bernstein. All Rights
Reserved.