|
Credit: J. Trauger (JPL/NASA)
JavaScript is always invoked through the Web bowser and usually is
intertwined with your HTML.
There are several ways to include JavaScript code in your HTML files:
- You can embed it between <SCRIPT> and </SCRIPT> tags.
See
this
for an example.
This is the usual method.
- A second common method is to include JavaScript code as
an event handler inside HTML elements in Forms. Typically,
the JavaScript code is usually a call to
a function which may carry out complex tasks. See
this
for the output of a
simple example.
- A third, less usual, method, is to use
<SCRIPT SRC=...> tags to read in JavaScript source
from a file. This is one way to (partially) get around the
limitation that Client-Side JavaScript cannot read data from
the server -- you can embed data as a JavaScript program.
However, the data is in an extremely limited format and
would usually be limited to a few hundred values.
You can also employ
this method to reuse code, to reduce the amount of code
you need write, and to reduce the amount of code you load, thus
improving browser performance.
For example, you can develop a library of commonly
used routines which are included in programs as the occasion arises,
or you can selectively load only those routines that a user
requires for a particular operation, as opposed to loading all
of the options which they might possibly use.
See this
for an example and this
for the source JavaScript code it calls.
- Another method is the JavaScript URL which lets you include
JavaScript code anywhere
an ordinary URL can be used. See this
for an example.
A subject related to the execution of JavaScript is the role of the
language element in the script tag. This acts to
shield versions of JavaScript code from browsers that cannot handle
the higher versions, which are currently 1.0, 1.2, and 1.2, and also to
shield it from other languages, such as VBScript. Consider the
following code fragments:
<script language="javascript1.0">
jsVer = 1.0
</script>
<script language="javascript1.1">
jsVer = 1.1
</script>
<script language="javascript1.2">
jsVer = 1.2
</script>
If a browser can only handle JavaScript 1.0, then only the first
script is executed and the variable "jsVer" is set to 1.0, since it will
never even see the next two scripts. But if it can handle higher
versions, jsVer will be set to 1.1 or 1.2. See
this for example.
|
|
 |
|
 |