Main Page

External file format

Although originally created for JavaScript, the
<script/>
tag can be used to specify any number of
different client-side scripting languages with the
language
attribute indicating the type of code being
used. For example,
language
can be set to
VBScript
to use Internet Explorer’s VBScript (Windows
only).
JavaScript code can be written free form within a
<script/>
tag, but only if the
src
attribute isn’t speci-
fied; when
src
is specified, the code inside a
<script/>
tag may not work (depending on the browser).
Example:
<html>
<head>
<title>Title of Page</title>
<script language=”JavaScript”>
var i = 0;
</script>
<script language=”JavaScript” src=”../scripts/external.js”></script>
</head>
<body>
<!-- body goes here -->
</body>
</html>
This example shows both inline JavaScript code and a link to an external JavaScript file. When using the
src
attribute, an external JavaScript file is referenced in the same way as images and style sheets.
By convention, external JavaScript files should have a .js extension, although it is not required by most
browsers (this leaves open the possibility of dynamically generating JavaScript code using JSP, PHP, or
another server-side scripting language).
External file format
External JavaScript files have a very simple format. Essentially, they are just plain text files containing
JavaScript code. No
<script/>
tags are needed inside of external files, because the
<script/>
tag ref-
erencing the file is present in the HTML page. This makes external JavaScript files look very similar to
source code files for other programming languages.
For example, consider the following inline code:
<html>
<head>
<title>Title of Page</title>
<script language=”JavaScript”>
function sayHi() {
alert(“Hi”);
}
</script>
</head>
<body>
<!-- body goes here -->
</body>
</html>
126
Chapter 5
08_579088 ch05.qxd 3/28/05 11:37 AM Page 126


JavaScript EditorFree JavaScript Editor     Ajax Editor


©