The function name can contain only alphanumeric characters (alphabet and digits) and the underscore. Also, the name cannot begin with a numeral.
Some valid function names
Some invalid function names
Remember, function names are case sensitive, thus, alert_box is not the same as Alert_box.
The function in itself does not do anything till we call it.
Calling a function is simple. You have to specify its name followed by the pair of parenthesis.
It’s good programming practice to place all functions in the HEAD section of the HTML document between the <SCRIPT> – </SCRIPT> tags. Function calls, on the other hand, can occur in any part of the document (where ever they are needed!), even inside event handler code.
The code below, calls the function we defined at the beginning of this session. This time we call it thru the onclick() event handler.
You’ll notice that a function call looks very similar to calling a method. Now, wasn’t that simple?
In the previous session, our event handler code for opening a new window had become very long. So instead of writing the code in the HTML tag, we shall place it inside a function and call this function from the event handler.
We name this function open_win and place it in the HTML head section.
What if you had ten links on a page each opening a new 400X200 pixel window with a different url? Writing separate code for each link can become quite a pain. A simple solution would be to create a function that opens the new window and call it through an event handler. However, we are still left with a problem! How do we instruct the same function to load a different url in each new window? Before we delve deeper into functions let us have a look at variables.
Contrary to popular belief, Apple didn't started in a garage. They made their first products in the bedroom of 11161 Crist Drive, Los Altos, the home of Steve Jobs' foster parents. In 1983, the number of the single-storey house was changed to 2066 when the land on which it stood was annexed by the city from the county. The company's first product was the Apple I computer. Fifty of these were created by Steve Wozniak in the bedroom and sold for $500 each to Paul Jay Terrell's Byte Shop. [more...]