Chapter 20
Script Example 3: Which Way Do We Go?

In This Chapter

Making the Grade

In this example, we’re going to look at a simple but important technique: browser-sensitive site navigation. Even though JavaScript is supported by the major Web browsers Netscape Navigator and Microsoft Internet Explorer, some users out there may be using a browser that does not support JavaScript (such as Lynx, Opera, or Ibrowse).

When you’re creating a Web site, it’s important for you to consider whether you want it accessible to all Web surfers or only those who use JavaScript-capable browsers. Of course, this depends in large part on the function of the site: If the site is a BlackJack game that’s written in JavaScript (like Chapter 22 of this book), it clearly is useful only to visitors with JavaScript-capable browsers. In fact, in the case of our BlackJack program, only JavaScript 1.1-capable browsers can play it.

On the other hand, perhaps you’re using JavaScript to “jazz up” a site. Yet, its basic content (information about you, your company, and so on) may be relevant to anyone. In this case, it’s sometimes best to create two versions of your content pages: one version for users without JavaScript and one for users with.

Parallel Universes

Imagine that your site consists of two “parallel universes”: one set of pages with graphics and JavaScript code and one set without JavaScript code (whether or not they contain graphics is up to you). We’ll say that each version of the site’s home page is named as follows:

All of the hyperlinks within home_nojs.htm also lead to non-JavaScript versions of pages, and the hyperlinks within home_js.htm lead to JavaScript versions of pages where appropriate.

The trick, then, lies in how to come up with a single URL that any surfer can visit and that will automatically sweep the user to the correct version of the home page.

He Went That-Away!

Solving this proposition requires the creation of a “dummy” page, which performs the redirection to the appropriate “real” home page, depending on the visitor’s browser.

Suppose your dummy page is named home.htm. The logic behind the redirection is based on these points:

When a JavaScript-capable browser loads this page, it executes the single line of JavaScript code and loads the new page into the window. A non-JavaScript browser can’t see this code and, therefore, continues down until it hits the <META> tag, which causes it to load a new specified page.

<body bgcolor="#FFFFFF">
<script language="JavaScript">
<!-- Hide from non-JavaScript browsers
location = "home_js.htm"

When a visitor loads this page (which we’ve named and publicized as home.htm, so the user doesn’t realize he’s actually opening a redirection page), a JavaScript browser will immediately open home_js.htm. Alternatively, a non-JavaScript browser will immediately open home_nojs.htm, and the deed is done. From that point on, everything else is taken care of by virtue of having employed the correct hyperlinks in each version of the home page.

You can easily test this example, even with Netscape Navigator or Internet Explorer. To do so, create the above dummy page and open it in Netscape. It should attempt to load home_js.htm (or whichever URL you decide to code). Then disable JavaScript in Navigator using the Network, Preferences, Language command and revisit the page. This time, it should try to load home_nojs.htm (or whichever URL you code for the non-JavaScript page).


The Least You Need to Know

In this short chapter, you learned the importance of audience-friendly Web sites. When offering content that is relevant to readers with any browser, it’s often wise to offer said content in non-JavaScript forms for users who don’t have capable browsers. By creating and publicizing a “dummy” home page, you can automatically redirect visitors to the JavaScript-enabled or -disabled true home page. All it takes is a one-statement JavaScript program and a <META> tag to redirect non-JavaScript browsers.

Previous Chapter Next Chapter

Beginning of ChapterTable of ContentsBook Home PageQue Home Page

For comments or technical support for our books and software, select Talk to Us.
To order books, call us at 800-716-0044 or 317-228-4366.

© 1997, QUE Corporation, an imprint of Macmillan Publishing USA, a Simon & Schuster Company.