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. Depending on your browser and operating system, you may be able to view the page source from a menu option or by right-clicking or control-clicking on the page.