Functions
• To keep
the browser from executing a script when the page loads, put 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.
Syntax
function
functionname(var1,var2,...,varX)
{
some code
}
Note: The
word function must be written in lowercase letters, otherwise a JavaScript
error occurs! Also note that call a function with the exact same capitals as in
the function name.
Example
<html>
<head>
<script type="text/javascript">
function displaymsg()
{
alert("Hello
World!");
}
</script>
</head>
<body>
<form>
<input
type="button" value="Click me!"
onclick="displaymsg()" /> </form>
</body>
</html>
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.