HTML and CSS Reference
In-Depth Information
HTML Comments within
Within a <script> section,
an HTML comment is
often added to hide
the JavaScript from old
browsers. The HTML
comment is a tag that
begins with <!-- and ends
with -->. If the HTML
comment is not closed
properly in the <script>
section, then that could
cause the remainder of the
Web page to not display in
the browser.
Table 9-6 JavaScript Section
General form:
<script src=”url” type=”valid MIME type” async=”async” defer charset=”character set” >
where script is the script tag, src specifies the location of an external script URL, type indicates
the valid MIME type or specific scripting language, language is a deprecated attribute in
HTML5, and defer is a Boolean attribute that indicates whether the script is going to generate
any document content. The type attribute is supported by HTML5 and the XHTML and XML
standards. The src, async, charset, and defer attributes are optional.
<script type=“text/javascript”>
<!- -Hide from old browsers
miscellaneous JavaScript code
To Enter the Start <script> and Comment Tags
The following step enters the <script> and HTML comment tags.
file chapter9-1.html,
click line 6
(a blank line).
In the Notepad++
javascript”> as
the beginning of the
script and then press
the e n t e r key.
insertion point
<!--Hide from
old browsers and
then press the e n t e r
key to put the insertion
point on line 8 (Figure 9-4).
Is it really necessary to add the comment, <!--Hide from old browsers?
Although JavaScript has been added to browsers since the second and third releases of Internet Explorer
and Netscape, conventional wisdom suggests that programmers take into consideration all possibilities,
which means, there may be someone still not using a JavaScript-capable browser. This may be especially
true in mobile devices.
Figure 9-4
Using JavaScript Variables
As in other programming languages, JavaScript uses variables to store values temporarily
in internal memory. A variable's value can change, depending on the results of an expres-
sion or data entered by a user from a form. Variables must have a unique name, and must
follow the same naming conventions as user-defined functions. JavaScript variable names
are case sensitive, which means the variable name months is different from the variable
name Months. Table 9-7 shows the rules with valid and invalid variable name examples.
JavaScript Comments
Comments can be added
to JavaScript in two
ways. The double slash
[//] is used to indicate a
comment for a single line.
The double slash [//] is also
used to close and hide
the end HTML comment
in the <script> section. To
comment multiple lines
with a script, begin the
comment with a slash and
asterisk [/*] and place an
asterisk and slash [*/] to
end the comment lines.
Search WWH ::

Custom Search