HTML and CSS Reference
In-Depth Information
JavaScript and Old or Disabled Browsers
2.6.1 Hiding JavaScript from Old Browsers
Is JavaScript Enabled? The answer is most probably “yes.” There are many ver-
sions of browsers available to the public and the vast majority of the public uses Firefox,
Opera or Internet Explorer. So why worry? Well, just because a browser supports Java-
Script does not mean that everyone has JavaScript enabled. There are also some older
text browsers that don't support JavaScript, but today it's more likely that JavaScript has
been disabled for security reasons or to avoid cookies than because the browser is old.
Cell phones, Palm handhelds, and speech browsers for the visibly disabled provide
browser support, but do not necessarily have JavaScript. There has to be some alterna-
tive way to address those Web browsers (see ).
Hiding JavaScript. If you put a script in a Web page, and the browser is old and doesn't
know what a <script> tag is, the JavaScript code will be treated as regular HTML. But if you
enclose JavaScript code within HTML comment tags, it will be invisible to the HTML ren-
derer and, therefore, ignored by browsers not supporting JavaScript. If the browser has Java-
Script enabled, then any HTML tags (including HTML comments) inserted between the
<script> </script> tags will be ignored. Hiding JavaScript in comment tags is a common prac-
tice used in JavaScript programs. It is introduced here so that when you notice these embed-
ded comments in other's programs, you will understand why. See Example 2.5.
<head><title>Old Browsers</title></head>
<body><font color="0000F">
<div align=center>
<script type="text/javascript">
<!-- Hiding JavaScript from Older Browsers
document.write("<h2>Welcome to Maine!</h2>");
// Stop Hiding JavaScript from Older Browsers -->
<img src="BaileyIsland.jpg" width="400" height="300" border=1>
<br />Bailey's Island
The JavaScript program starts here. Browsers that don't support JavaScript will
skip over this opening <script> tag.
The <!-- symbol is the start of an HTML comment and will continue until --> is
reached. Any browser not supporting JavaScript will treat this whole block as a
comment. JavaScript itself uses two slashes or C-style syntax, /* */ , and will ignore
the HTML comment tags.
Search WWH ::

Custom Search