HTML and CSS Reference
In-Depth Information
2.6
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 http://www.internet.com ).
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.
EXAMPLE 2.5
<html>
<head><title>Old Browsers</title></head>
<body><font color="0000F">
<div align=center>
1
<script type="text/javascript">
2
<!-- Hiding JavaScript from Older Browsers
3
document.write("<h2>Welcome to Maine!</h2>");
4
// Stop Hiding JavaScript from Older Browsers -->
5
</script>
<img src="BaileyIsland.jpg" width="400" height="300" border=1>
<br />Bailey's Island
</div></font>
</body>
</html>
EXPLANATION
1
The JavaScript program starts here. Browsers that don't support JavaScript will
skip over this opening <script> tag.
2
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.
Continues
 
 
 
 
Search WWH ::




Custom Search