1. Computing

Site Navigation

9. Not Found

clr gif

When you use a combo box for navigation there is the possibility that your visitors will enter something themselves that does not correspond to the name of a page on your site. We need to work out a way to verify that what they have entered is a valid page name if we want to display an appropriate error page if there is no page corresponding to what they entered. There are three ways to do this.

I am going to assume that all of the pages are in a given directory and that there are no other pages in the directory except perhaps the page with the navigator on it. This is the best way to set this up.

Solution one if you have the appropriate access is to set up a custom not found page for the directory that will display when the page that the script is searching for is not found. Doing this will not require any changes to the script.

Solutions two and three both involve modifying the script so that it looks up an array that contains all of the valid pages. If the page is not found in the array then the error page will be displayed. The code now becomes too complex to include within the page directly so the first thing to do is to create a function to perform the lookup.

function validPage(page) {
for (var i=0; i<page_array.length; i++) {
if (page == page_array[i]) return page;}
return 'notfound.htm';
}

The above function along with the declaration of the array of valid pages (which we will get to in a minute) goes in the head section of your page. You may want to modify the file name mentioned in the script if your not found page is not called notfound.htm.

The next step is to actually create the array and add it above the function in the head section of the page. This is where solutions two and three differ. If you have access to use a server side language such as PHP you can incorporate some server side code into your script to look up the filenames for you to automatically load the array. Here is an exapmle of how to do this using PHP (not that your web server must support PHP and the file containing this code must have a .php suffix instead of .htm or .js for this to work correctly).

var page_array= new Array();
var entry = 0;
<?php
$page_handler = opendir(".");
while( $file = readdir($page_handler) )
if( $file != "." && $file != ".." )
echo "page_array[entry++] = '$file';\n";
?>

If you use this method then you call the validPage function from within the onclick by modifying the code as follows:

location = ''+ validPage(document.ex.nav.value+'.htm');

If you don't have access to use server side scripting then solution three is to code the array manually. The first thing to do is to remove the .htm from the the notfound.htm reference in the function since we can make maintaining the array simpler by adding this on at the end. The code that you need to set up the array looks like this:

var page_array = new Array('page1','page2','page3');

Just include as many page names as you need separated by commas. Each entry can start on a new line if necessary. Unfortunately you'll need to update the code whenever you add or delete pages from the navigation with this method.

With this method you call the validPage function from within the onclick by modifying the code as follows:

location = ''+ validPage(document.ex.nav.value)+'.htm';
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
Related Video
Zip File 101
Computer Cable Ports 101

©2013 About.com. All rights reserved.