Page 23 - LoudOffice_Guide-to-HTML_Part-II_Advanced.PDF
P. 23

Defining Functions
               The function definition is a statement that describes the function: its name, any values
               (known as "arguments") which it accepts, and the statements of which the function is
               comprised.


                       f u n c t i o n  f u n c N a m e ( a r g u m e n t 1 , a r g u m e n t 2 , e t c )
                       {  s t a t e m e n t s ;  }
               A function doesn't necessarily require arguments, in which case you need only write out the
               parenthesis; e.g. f u n c N a m e ( ) . If you do specify arguments, those arguments will be
               variables within the function body (the statements which make up the function). The initial
               values of those variables will be any values passed on by the function call.

               Generally it's best to define the functions for a page in the < h e a d >  portion of a document.
               Since the < h e a d >  is loaded first, this guarantees that functions are loaded before the user
               has a chance to do anything     that might call a function. Alternately, some programmers
               place all of their functions into a separate file, and include them in a page using the s r c
               attribute of the < s c r i p t >  tag. Either way, the key is to load the function definitions before
               any code is executed.
               Some functions may return a value to the calling expression. The following function accepts
               two arguments, x and y, and returns the result of x raised to the y power:


                       f u n c t i o n  r a i s e P ( x , y )  {
                         t o t a l = 1 ;
                         f o r  ( j = 0 ;  j < y ;  j + + )  {
                            t o t a l * = x ;
                         }
                         r e t u r n  t o t a l ;  / / r e s u l t  o f  x  r a i s e d  t o  y  p o w e r
                       }
               Calling Functions
               Functions can be called by calling their name within another JavaScript form, or on the
               occurrence of an ‘Event’ (discussed below).      For example, if you had a function named
               A l e r t T h a n k Y o u ( )  that was tied to the o n S u b m i t  event on a form, it would be called as
               follows:
                                    < f o r m  a c t i o n = ” h e l l o . c g i ”  m e t h o d = ” p o s t ”
                                           o n S u b m i t = ” A l e r t T h a n k Y o u ( ) ; ” >


               Document Object Model
               Often referred to as the DOM, this object model is a hierarchy of all objects "built in" to
               JavaScript. Most of these objects are directly related to characteristics of the Web page or
               browser. The reason we qualify the term "built in" is because the DOM is technically
               separate from JavaScript itself. That is, the JavaScript language specification, standardized
               by the ECMA, does not actually specify the nature or specifics of the DOM. Consequently,
               Netscape and Microsoft have developed their own individual DOM’s that are not entirely
               compatible. Additionally, the DOM stands apart from JavaScript because other scripting
               languages can theoretically access it as well, such as VBScript in Internet Explorer.       In





               LoudOffice.com Guide to HTML – Part II                                               Page 23
   18   19   20   21   22   23   24   25   26   27   28