JavaScript Functions
A function will be executed by an event or by a call to the function.
JavaScript Functions
To keep the browser from executing a script when the page loads, you can put your script into a function.
A function contains code that will be executed by an event or by a call to the function.
You may call a function from anywhere within a page (or even from other pages if the function is embedded in an external .js file).
Functions can be defined both in the <head> and in the <body> section of a document. However, to assure that a function
is read/loaded by the browser before it is called, it could be wise to put functions in the <head> section.
How to Define a Function
Syntax
function functionname(var1,var2,...,varX)
{
some code
} |
The parameters var1, var2, etc. are variables or values passed into the function. The { and the } defines the start and end of the function.
Note: A function with no parameters must include the parentheses ()
after the function name.
Note: Do not forget about the importance of capitals in JavaScript!
The word function must be written in lowercase letters, otherwise a JavaScript
error occurs! Also note that you must call a function with the exact same
capitals as in the function name.
JavaScript Function Example
Example
<html>
<head>
<script type="text/javascript">
function displaymessage()
{
alert("Hello World!");
}
</script>
</head>
<body>
<form>
<input type="button" value="Click me!"
onclick="displaymessage()" />
</form>
</body>
</html> |
|
If the line: alert("Hello world!!") in the example above had not been put
within a function, it would have been executed as soon as the line was loaded.
Now, the script is not executed before a user hits the input button. The function displaymessage()
will be executed if the input button is clicked.
You will learn more about JavaScript events in the JS Events chapter.
The return Statement
The return statement is used to specify the value that is returned from the function.
So, functions that are going to return a value must use the return statement.
The example below returns the product of two numbers (a and b):
Example
<html>
<head>
<script type="text/javascript">
function product(a,b)
{
return a*b;
}
</script>
</head>
<body>
<script type="text/javascript">
document.write(product(4,3));
</script>
</body>
</html> |
|
The Lifetime of JavaScript Variables
If you declare a variable within a function, the variable can only be accessed within that function. When you exit the function, the
variable is destroyed. These variables are called local variables. You can have local variables with the same name in different functions, because each is
recognized only by the function in which it is declared.
If you declare a variable outside a function, all the functions on your page can access it. The lifetime of these variables starts when they are declared, and
ends when the page is closed.
 |
More Examples |
Function with a
parameter
How to pass a variable to a function, and use the variable in the function.
Function that returns a value How to let
a function return a value.
Reliable, Affordable, Feature-Rich Web Hosting!
Take the uncertainty out of Web hosting and let
GoDaddy.com put service, performance and value back in. No matter which
hosting type or plan you choose, your site receives 24/7
maintenance and protection in our world-class data center. Plus,
you get the expert, friendly service you deserve, from the
world's largest hostname provider.
With three plans to choose from and
prices starting at just $4.99 per month, GoDaddy.com is sure to have a plan that's
right-sized and right-priced just for you!
All plans feature FREE 24x7 setup, FREE 24x7 monitoring, best-
of-breed routers, firewalls and servers, 24x7 onsite physical security
and access to our exclusive Go Daddy Hosting Connection, THE place
to install over 30 FREE applications. Virtual Dedicated and Dedicated
Server plans also available.
Visit GoDaddy.com today.
Virtual Dedicated, Dedicated Server and unlimited plans also available.
Save 20% on 12 months or more of shared web hosting - Enter code w3s20off at checkout
|