Assignment010

JavaScript Methods - Prompt

JavaScript is a scripting language that uses objects. For example, your browser window is an object. We can refer to this object via JavaScript by using "window."

The following is a method (action) of the window object. This method is envoked by using the following code:

window.prompt("Please enter your name.", "");

The prompt reply is saved to a variable by using the following code:

var name ;
name = window.prompt("Please enter your name.", "")' ;

The first line creates a variable by the name of 'name.' The second line sets 'name' equal to the reply of the prompt.

Welcome to my Web page, null!

After the user inputs a reply to the prompt, you can use the document.write command to write to the Web page. In this example, the prompt reply 'null' is included by combining text with a variable. You will learn about variables in the next assignment.

Here's the code to generate the prompt:

<script language="JavaScript" type="text/javascript">
//Create the variable.

var name;

//Ask the user to type in their name.
name = window.prompt("Please enter your name.", "");

//Write the name to the document.
window.document.write("<strong>Welcome to my Web page, " + name + "!</strong>");

window.document.write("<p>After the user inputs a reply to the prompt, you can use the <strong>document.write</strong> command to write to the Web page. In this example, the prompt reply '" + name + "' is included by combining text with a variable. You will learn about variables in the next assignment.") ;

</script>