JavaScript Functions
You define a function with a name. Then you can call the function by the name, along with any inputs (known as parameters) it will need.
The code snippet, below, illustrates how a function works, at its most basic level. This function is called "greeting." It takes one argument, called "Salutation." The user gives it a value (in this case "Hello Betsy") and it displays that message back to the user on the page: In the head:
<script>
var greeting = function(Salutation){
document.open();
document.write(Salutation);
document.close();
};
</script>
And in the body:
<<body onload="greeting('Hello Betsy')">
Example
Functions may have multiple parameters:
<script>
var greeting = function(Salutation01, Salutation02, Salutation03){
document.open();
document.write(Salutation01);
document.write(Salutation02);
document.write(Salutation03);
document.close();
};
</script>
...
<body onload="greeting('Hello Betsy', ' and Bob', ' And Frank!')">
You don't necessarily have to give a JavaScript function a value:
<script>
var greeting = function(){
document.open();
document.write("Hello World");
document.close();
};
</script>
...
<body onload="greeting()">