Javascript
Related applications
© The scientific sentence. 2010
|
Functions
1. Definitions:
Functions can be defined both in the <head> and in the
<body> section of a document. When they are defined in the
head section, they are loaded, then known before any call.
The code inside a function is exected only when the function
is called orperformed by an event; otherwise, the code is executed
during the loading of the page.
The function can be called from anywhere within the page, and even
from another page that contains the link to the related external
file.js.
2. Example:
<html>
<head>
<script type="text/javascript">
function any_function()
{
... some code
}
</script>
</head>
<body>
any_function();
<form>
<input type="button" value="submit" onclick ="any_function()" >
</form>
</body>
</html>
The function any_function is called and executed in the line: any_function();
and
by the form using the event onclick.
3. Two kind of functions:
Without parameters as the function any_function() defined above
With parameters the returns a value as:
<html>
<head>
<script type="text/javascript">
function sum_function(a, b)
{
c = a + b;
returns c;
}
</script>
</head>
<body>
result = sum_function(5,6);
The variable "result" holds the value 11.
</body>
</html>
The variable "c" is a local variable; that is declared
within the function. It is not recognized outside this function.
All of the functions defined in the page can use the same names
for these variables. When a variable is declared outside a function,
all the other functions can access it. This variale lasts the time
the page is loaded.
|
|
|