![]() |
|
![]() ![]() ![]() |
|
Example 2: Parsing the Location String
Back on the Strings page, I promised to show you a script that extracted the path and file name for the current page. Here's that script: <script language="JavaScript"> <!-- /* This script gets the URL of the current page, then extracts the file name and path from the URL */ var URL = unescape(location.href) // get current URL in plain ASCII var xstart = URL.lastIndexOf("/") + 1 var xend = URL.length var hereName = URL.substring(xstart,xend) var herePath = URL.substring(0,xstart) document.write("The name of the current file is: " + hereName) document.write("The path of the current file is: " + herePath) // --> </script> Now let's think about how we might build on this script to do something useful. Consider the Previous and Next buttons on this page (and so many others); if we name files systematically, we should be able to use a JavaScript to generate the HREF values for the links automatically, instead of hand-coding them. Rather than hard-code most of the filename, I'm going to make this script as flexible as possible by getting current file name in the way I did in the script above and then build the previous and next file names from it. That way, I'll be able to use this script in any other project that uses the same numbering scheme. For my script, I'm using the following file naming system: Note that the script could be modified to handle slightly different naming schemes, or to handle sets that include more than 100 files (the limit for this one) Here's the code: <script language="JavaScript"> <!-- /* Script creates the finds the location of the previous and next files in a regularly numbered set of HTML files that all reside in the same directory. One function sets the location to the previous file, the other to the next file. These functions can be used for Previous and Next page links by setting the HREF values in the respective A tags to javascript:previousFile() and javascript:nextFile(). Note that this script should be placed in the HEAD section of the file, to insure that the functions are loaded before the links that call them. */ var URL = unescape(location.href) // get current URL in plain ASCII var xstart = URL.lastIndexOf("/") + 1 var xend = URL.length var digitOnePlace = URL.lastIndexOf('.') - 2 var digitTwoPlace = URL.lastIndexOf('.') - 1 var digitOne = URL.charAt(digitOnePlace) var digitTwo = URL.charAt(digitTwoPlace) var filePrefix = URL.substring(xstart,digitOnePlace) var suffixStart = URL.lastIndexOf('.') var fileSuffix = URL.substring(suffixStart,xend) /* The following lines provide a workaround for the JS implementation in Opera 3.0, which treats all elements of a string as string values, and therefore cannot perform arithmatical operations on them correctly; to get around this, I create new variables with number values equivalent to the values of digitOne and digitTwo */ var dig1 = null var dig2 = null dig1=parseInt(digitOne) dig2=parseInt(digitTwo) function previousFile() { if (dig2 == 0) { dig2 = 9 dig1-- } else { dig2-- } previousFileName = filePrefix + dig1 + dig2 + fileSuffix location.href = previousFileName } function nextFile() { if (dig2 == 9) { dig2 = 0 dig1++ } else { dig2++ } nextFileName = filePrefix + dig1 + dig2 + fileSuffix location.href = nextFileName } // --> </script> if you view the page source, you'll see that I'm using this code for the previous and next buttons in these pages, but setting the HREF values for the links equal to javascript:previousFile() and javascript:nextFile(). (I've added some additional comments in the sample code here.) |
|
![]() ![]() ![]() |
|
|