WebDeveloper.com �: Where Web Developers and Designers Learn How to Build Web Sites, Program in Java and JavaScript, and More!   
Web Developer Resource Directory WebDev Jobs
Animated GIFs
CSS
CSS Properties
Database
Design
Flash
HTML
HTML 4.01 Tags
JavaScript
.NET
PHP
Reference
Security
Site Management
Video
XML/RSS
WD Forums
 Client-Side
  Development

    HTML
    XML
    CSS
    Graphics
    JavaScript
    ASP
    Multimedia
    Web Video
    Accessibility
    Dreamweaver
    General
    Accessibility
    Dreamweaver
    Expression Web

    General

 Server-Side
  Development

    PHP
    Perl
    .NET
    Forum, Blog, Wiki & CMS
    SQL
    Java
    Others

 Site Management
    Domain Names
    Search Engines
    Website Reviews

 Web Development
  Business Issues

    Business Matters

 Etc.
    The Coffee Lounge
    Computer Issues
    Feedback




JavaScript for the Holidays, Part II

by Heidi Brumbaugh

In the for statement, the first expression (i=0) is the initial condition. The next expression (i<=10) is the condition. If it evaluates to true, the statements inside the loop execute. Otherwise, the loop is terminated and the program advances to the first line after the loop's closing bracket. The final expression (i++) is the increment expression. Here, the program adds 1 to i each time through the loop.

This is a good way to generate a menu. To implement Next/Prev buttons like the reader wants, you would need to write a routine that checks the current HTML filename and adds (or subtracts) 1 from it. These buttons are implemented in the sample file. Here's the code that does the job:

 function prev() { // Go to previous file var prevFile = getAdjustedFile(-1) self.location.pathname = prevFile } function next() { // Go to next file var nextFile = getAdjustedFile(1) self.location.pathname = nextFile } function getAdjustedFile(inc) { // Returns adjusted file name, increasing or // decreasing number in name. Assume we know // base file name is "file.htm" // Set upper and lower boundaries so we don't try // to load a file that doesn't exist. var lowerBound = 0 var upperBound = 2 var fileNumber // First, read the full pathname of the file. // This looks something like this: // webdeveloper/javascript/1file.htm var fullURL = self.location.pathname // Remember "left part" of URL. var leftPart = fullURL.substring (0,fullURL.lastIndexOf("/")+1) // Now pull out number between that // last slash and "file.htm" and convert // it to an integer. The parseInt function // returns a number from a string. The substring // function returns just part of the string, and // the lastIndexOf and indexOf functions tell where // in the string to get the value. fileNumber = fullURL.substring (fullURL.lastIndexOf("/")+1,fullURL.indexOf("file.htm")) fileNumber = parseInt(fileNumber) // Check to make sure we got an integer // (otherwise don't try to load file). if (!isNaN) { // Value isn't an integer; return same filename. return fullURL; } else { var newNumber = fileNumber + inc // Now make sure there's a file before or after this one. if ((newNumber upperBound)) { // New value would be out of range. return fullURL; } else { // Value is in range; make adjustment and // put string back together. return leftPart + newNumber + "file.htm" } } } 

These routines show how to do the numeric/string manipulation, but the functionality is fairly minimal. Improvements might include disabling/enabling the buttons if the next (or previous) file in sequence were out of bounds, moving the buttons to a separate frame so you didn't have to duplicate the code, and reassembling the entire URL, not just the path.

[Jump to Part 3]


Web Developer® Site Feedback
Web Developer®
Copyright © 1999-2001 internet.com Corp.
All rights reserved.


HTML5 Development Center


Recent Articles