Prompting for Information with JavaScript

By Herbert J. Bernstein
© Copyright 2004 Herbert J. Bernstein

JavaScript Prompting

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.

Your browser does not support JavaScript


<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 &deg;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 + 
                    "&deg;F is " +
                    tempK +
                    "&deg;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.