Main Page

noscript tag

on the following line. Next, you see the function definition as usual. The second-to-last line is the most
interesting because it starts with a JavaScript single-line comment (the two forward slashes) and then
continues with the close of the HTML comment (
-->
). This line is still considered part of the JavaScript
code, so the single-line comment notation is necessary to avoid a syntax error. However, older browsers
only acknowledge the close of the HTML comment and, therefore, ignore all the JavaScript code. A
browser that supports JavaScript, however, just ignores this line and continues on to the closing
</script>
tag.
Although this method of code-hiding was very prevalent in the early days of the Web, it is not as neces-
sary today. Presently, most of the popular Web browsers support JavaScript, and those that don’t often
are smart enough to ignore the code on their own. It is completely up to you whether you choose to use
this method, but keep in mind that using external JavaScript files inside of inline code is a much easier
method of hiding code from older browsers.
The <noscript/> tag
Another concern over browsers without JavaScript is how to provide alternate content. Hiding the code
was part of the solution, but developers wanted a way to specify content that should appear only if
JavaScript wasn’t available. The solution came in the form of the
<noscript/>
tag, that can contain any
HTML code (aside from
<script/>
). This HTML code is ignored by browsers that support JavaScript
and have it enabled; any browser that doesn’t support JavaScript or has it disabled renders the content
of
<noscript/>
. For example:
<html>
<head>
<title>Title of Page</title>
<script language=”JavaScript”>
function sayHi() {
alert(“Hi”);
}
</script>
</head>
<body>
<script language=”JavaScript”>
sayHi();
</script>
<noscript>
<p>Your browser doesn’t support JavaScript. If it did support
JavaScript, you would see this message: Hi!</p>
</noscript>
<p>This is the first text the user will see if JavaScript is enabled. If
JavaScript is disabled this is the second text the user will see.</p>
</body>
</html>
In this example, the
<noscript/>
tag is included with a message telling the user that the browser
doesn’t support JavaScript. Chapter 8, “Browser and Operating System Detection,” explains a practical
way of using
<noscript/>
.
130
Chapter 5
08_579088 ch05.qxd 3/28/05 11:37 AM Page 130


JavaScript EditorFree JavaScript Editor     Ajax Editor


©