Web Design with HTML and CSS

(National Geographic (Little) Kids) #1
Interactivity on the web

Lesson 10, Introduction to Interactivity 215

5 Add the following code (highlighted in red):


<script type="text/javascript">
function show_prompt()
</script>
A function in JavaScript is code that will be executed by an event on the page. In this
case, the code is called show_prompt(), and it is unique code that tells your web
browser to open a small pop-up window. The event that triggers this function is the user
clicking the Submit button.
The show_prompt() function needs more information to work.

6 Add the following code (highlighted in red) below the function:


<script type="text/javascript">
function show_prompt()

{
var name=prompt();
}

</script>
In this line of code, you have declared a variable and its value. This variable, called name,
obtains its value from the prompt function. One line of code is the minimum amount of
information you need to make something happen in your JavaScript.
To trigger the JavaScript code, you need to add an instruction to your HTML button
that describes how to trigger the code and what function to use.

7 Add this code to the HTML for your button (highlighted in red):


<input type="button" onclick="show_prompt()" value="Submit" />
The onclick code is known as a JavaScript event and the value “show_prompt()” is
the JavaScript function that you declared in step 5 in your <script> tag. Now you have
completed a logical chain that essentially says “When a user clicks on this Submit button,
call the show_prompt function. When the show_prompt function runs, it will call
another function named prompt.
Free download pdf