Jim Hanson's | ![]() |
JavaScript Functions
Make sure to have
<!-- open commentbetween the SCRIPT tags.
then your JavaScript code
// and a close to the comment -->
While the SCRIPT tags won't be processed by browsers that don't recognize them, the HTML comment block is needed to "hide" the comment from the non-JavaScript capable browsers. (Otherwise they will just display the JavaScript as text. The JavaScript processors will ignore the HTML comment symbols.)
JavaScript statements can either be executed as soon as encountered in your page by the browser, or deferred for later execution, when called as a result of some action. JavaScript functions are in this second group. Normally you want to include JavaScript functions between the <HEAD> and </HEAD> tags on your page. This insures that the functions will be completely loaded before they can be executed by some action specified in the body of the page.
In this section we will concentrate on JavaScript functions.
JavaScript functions bear a strong resemblance to C programming language functions. If you can program in C, you will have little trouble with JavaScript. But there are significant differences.
Function format: function_name (arg_type argument ) |
Function format same except: ; is optional, required only if more than one statement on a line arg_type is not used |
function's arguments are typed | function's arguments are not typed |
Called functions need prototype | No function prototypes |
Pointers (addresses) can be used | No pointers allowed |
Variables must be explicitly typed as int, float, char, etc. | Variables don't need to be typed, just assigned a value or declared with a var
Variable Names |
No inherent objects | Many predefined objects with methods and properties |
Must be compiled before execution | No compilation. Each statement is processed by interpreter every time it is used. |
All JavaScript statements and functions in a page need to be enclosed in <SCRIPT> tags to flag them to the JavaScript interpreter. You should always include the parameter LANGUAGE="JavaScript" since LANGUAGE="VBScript" is also starting to be seen. HTML comment tags prevent the script contents from being printed out by browsers that don't handle JavaScript. The format is:
<!-- <SCRIPT LANGUAGE="JavaScript"> tags are ignored by browsers without JavaScript capability -->
<SCRIPT LANGUAGE="JavaScript">
<!-- HTML comment tags hide JavaScript statements from browsers that can't handle them
// JavaScript statements and comments are visible to JavaScript
// capable browsers (these two lines are JavaScript comments)
... JavaScript functions or statements ...
// end the HTML hiding -->
</SCRIPT>
While the LANGUAGE parameter in the SCRIPT tag is optional, it is a good idea to include it, since other SCRIPT types may be used in the future.
For a more complete description of JavaScript, you can visit Netscape's JavaScript Authoring Guide.
NOTE: A new browser window was opened for viewing these pages. To return, you can just select another browser (from the task bar in Windows 95 or click your mouse somewhere on another window). You can also close (or File, Exit) from this window when done, leaving only the calling window. |
Member of the Internet Link Exchange | Free Home Pages at GeoCities |