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.
The first major sporting event to be broadcast live on YouTube was the 2010 Indian Premier League. All 60 cricket matches were free for the world to watch. [more...]