Simple button and function example

This page contains a button that calls a user-defined function defined in the <head> section of the page.

 


 

The HTML code for this button is:

<input type="button" value="Click here!" onClick="greetUserByName()">

The JavaScript code for the greetUserByName() function in the <head> section of the page is:


<script>
function greetUserByName()
{
    var name = prompt("Please enter your name.");
    alert("Hello, " + name + "!");
}
</script>
    

View the source for this page to see the function and button definitions in their full context.