One way in which I decided to try and improve navigation around the site was to include a menu at the bottom of the pages that are a continuation of another. For example, the "Postcards" section of "Terre Haute" currently contains 11 pages, the "Castle" section of "Bristol, England' currently contains 6 pages.
What I came up with looks like this...
I've seen menu's like this on other sites but in this case decided simply to write my own.
What I came up with is...
var path = location.pathname;
path = path.replace(/\\/g, '/');
var fullName = ""
var fileName = path.substring(path.lastIndexOf('/')+1);
var numStart = fileName.search(/\d/);
var numEnd = fileName.indexOf('.');
var pageNum = fileName.substring(numStart,numEnd);
var firstPart = fileName.substring(0, numStart);
case "tpcards" :
fullName = "Postcards";
case "bcastle" :
fullName = "Bristol Castle";
document.write('<p class="ctr">' + fullName + ': ');
document.write('<a href="' + firstPart + '1.htm">First</a>');
document.write(' <a href="' + firstPart + (pageNum-1) + '.htm">Prev.</a>');
document.write(' <a href="' + firstPart + cnt + '.htm">' + cnt + '</a>');
document.write(' ' + cnt);
document.write(' <a href="' + firstPart + (++pageNum) + '.htm">Next</a>');
document.write(' <a href="' + firstPart + maxPages + '.htm">Last</a>');
location.pathname is used to obtain the path from the root and the current document name. For example, /th/tpcards6.htm
path = path.replace(/\\/g, "/"); is for when the site is on a CD or hard drive. What this code does is replace all the instances of "\" in the path name and changes them to a "/". This is the regular expression form of replace that is capable of changing all the looked for characters in a string.
path.substring(path.lastIndexOf('/')+1); looks for the last instance of the substring "/" in the variable named path. and returns the position number from the left. The first character of a string starts at position 0. ie lastIndexOf() starts the search from the right of the string. In this instance it's extracting the filename from the full path.
fileName.search(/\d/); looks for any numeral in the string variable fileName and returns its position number. This is a regular expression, where \d means digit. \d\d would look for a substring of two digits. Because each of the files is named to a convention ie tpcards1.htm, tpcards2.htm etc. it's picking out the position at where the digits start. In tpcards12.htm it would return the position of the first digit it finds. In all these examples it would return the number 7.
fileName.indexOf('.'); looks for the first instance of the substring "." in the variable named fileName and returns the position number from the left. The first character of a string starts at position 0. ie IndexOf() starts the search from the left of the string. In tpcards1.htm it would return 8, in tpcards12.htm it would return 9.
fileName.substring(numStart,numEnd); Uses the previous two searches to extract the number in the file name. In tpcards1.htm it would return 1, in tpcards12.htm it would return 12.
fileName.substring(0, numStart); Extracts the first part of the filename. In tpcards1.htm and tpcards12.htm it returns "tpcards"
The final part of the code simply writes the links and page numbers to the HTML page. The actual page numbers are inserted using a FOR loop
Calling the function
Where the menu is to appear in the BODY of the page the following code is used...
Object: Document - Document objects and properties
This page created 19th October 2005, last modified 19th October 2005