HTML and CSS Reference
In-Depth Information
The second step is to create a link from the HTML source file to the external JavaScript document.
This is handled through a <script> tag with an src attribute, typically in the <head> section of
the main document. Say that the previously created JavaScript file was saved as main.js . To link or
include the JavaScript file, use this code:
<script type=”text/javascript” src=”scripts/main.js”></script>
You'll notice two things right away. First, in addition to the src attribute, the type attribute still
defines the kind of script as JavaScript. Second, the <script> tag is empty, that is, there is no con-
tent between the opening and closing tags. The path to the JavaScript file in the src attribute can be
document relative (as it is here) or absolute, like http://mySite.com/scripts/main.js .
When the page is rendered in the browser, there is no indication that you're working with multiple
files. The functionality loads exactly the same, as shown in Figure 22-1.
FiGure 22-1
coMMenTinG JaV
Ja V
VV
scriPT code
Occasionally it is helpful to add comments to your JavaScript code, especially when
you externalize the files. You have two ways to create a JavaScript comment. To
create a single-line comment, place two forward slashes, // , at the beginning of the
code line, like this:
// This function gets the current time
You can also use the two-slash method at the end of a code line; all the text that
follows is considered a comment and is ignored by the JavaScript engine.
For multiple line comments, start your comment with a slash, followed by an asterisk,
/* , and end it with the reverse: an asterisk, followed by a slash, */ . Here's an example:
/*
This function gets the current time
and presents it in an AM/PM format.
*/
Although it is not necessary to put the /* and */ characters on their own line, it
does make the comment much more noticeable.
Search WWH ::




Custom Search