Sams Teach Yourself JavaScript in 24 Hours

Sams Teach Yourself JavaScript in 24 Hours

By Michael Moncur

Supporting Non-JavaScript Browsers

What about browsers that don't support JavaScript at all? You can't detect them with a script because they won't even run the script. However, there are ways of dealing with these browsers.

With 99% of Web users using a recent copy of Netscape or IE, where are the non-JavaScript browsers? First, there are still some older browsers out there, and text browsers such as Lynx don't support scripting. Second, and more importantly, many Netscape and IE users have JavaScript support turned off, either due to security concerns or just to avoid watching scrolling messages in the status line.

In the last few years, a wide variety of new browsers have also emerged. Palmtop computers, such as 3Com's PalmPilot and the Windows CE platform, support Web browsing, as do some cellular phones and other appliances. Since none of these support JavaScript, it's even more important to provide an alternative when possible.

One way to be friendly to non-JavaScript browsers is to use the <noscript> tag. Supported in Netscape 3.0 and later, this tag displays a message to non-JavaScript browsers. Browsers that support JavaScript ignore the text between the <noscript> tags, while others display it. Here is a simple example:

<noscript>
This page requires JavaScript. You can either download a copy
of netscape from <a href="http://www.netscape.com/"> their page,
or switch to the <a href="nojs.html">Non-JavaScript</a> version of
this page.
</noscript>

The main problem with using <noscript> is that Netscape 2.0 will display the text between the tags, even though it does support some JavaScript. While Netscape 2.0 isn't too common these days, there is a way to solve this problem. The following script detects non-JavaScript browsers and displays a message:

<script LANGUAGE="JavaScript" type="text/javascript">
     <!-- //hide from old browsers
     ...script commands go here...
     /* (start JavaScript comment, end HTML comment)
     -->
     You're using a non-JavaScript browser! Shame on you.
     <!-- */ // -->
</script>

This uses the HTML comment tags (<!-- and -->) to hide the script commands from old browsers. It then uses JavaScript comment tags (/* and */) to enclose the message for non-JavaScript browsers, so it will be disregarded by browsers that support JavaScript.

An easier alternative is to send users with JavaScript support to another page. This can be accomplished with a single JavaScript statement:

<script LANGUAGE="JavaScript" type="text/javascript">
window.location="JavaScript.html";
</script>

This simply sends the user to a different page. If the browser doesn't support JavaScript, of course, the script won't be executed.

The final alternative is to simply make your scripts unobtrusive. Use comments to hide them from other browsers, as described in Hour 3, "How JavaScript Programs Work," and be sure they're not essential to navigate or read the page.

Share ThisShare This

Informit Network