SCRIPTING:

Nadja Auermann

MEMORIZE PREVIOUS AND NEXT ENTRY WHEN LOOPING MULTIPLE ARRAYS
A Most wanted subroutine: how to implement a function which returns the previous or next entry of an array accordingly to which entry was grabbed in the previous attempt or at a previous time.
Can loop either backward or forward. If an array edge is reached, it restarts from the other edge.
Works with an undefined amount of arrays.
September 2001
{ @ }

The model above is Nadja Auermann
LOADS OF NADJA AUERMANN ON THE NET


Build reference objects before using subroutine
These objects and subroutine must be included first, since they will allow you to get previous/next entries from an undefined amount of arrays: using them, in other words, will spare you the necessity to build as many copies of the main subroutine as many arrays whose previous/next entries you need grab.

The fairly simple code fragments below will build an object and then an array whose each entry will permanently store all the arrays you pass to the main subroutine and for each of them the level at which you stopped (in order to update it later when the command to grab the next or previous entry from given object is performed).
As you can see function _listedArray includes two properties:
  1. array which will store the name of the array
  2. level which will store the level grabbed as last.
Each object will thus be entirely independent of the others and safely referred.
The _arraysCollection variable will be an array which will collect all the objects built by the main subroutine: the main subroutine will then grab the correct entry accordingly to the array parameter passed to it (see section 2)

function _listedArray(array,level){
this.array=array;
this.level=level;
}

var _arraysCollection=new Object();

Note the leading underscore in the name _arraysCollection and in the name _listedArray: this is to make the names unlikely to conflict with other variables in the page that you might have set with the same name (arraysCollection without a leading underscore char would not be so uncommon a name in a big script, perhaps. Better safe than sorry I guess).

Since november 2004, the main script function included below will also do the following:
If you forget to include this variable _arraysCollection, the function will now detect it is missing and will create it on the fly. It will also create _listedArray on the fly if it detects it was not added.
In other words, you could now forget the variables above and just use the function below.


The main Subroutine
Main subroutine and how it works and how must be invoked

Hre is the main subroutine:



Here is how it works:
  1. You have to pass as first argument the array you want to loop as an Object - that is, namely being careful not to put it in between quotes.
    Thus if you're invoking the subroutine from within a button you'd use:
    onClick="arrayScaling(arrayName)"
  2. The second argument called backOrForw flags whether the event must perform a forward or a backward task:
    1. Do not set it or set it to zero for going backward
    2. To go forward set it to one.
  3. The subroutine will then check if an object with that name is already in the collection, if not it will build it and add it to the collection.
  4. It will then grab its current level property and perform the backward or forward task accordingly to the backOrForw argument value.
  5. The subroutine returns the grabbed entry, thus if you want to assign it to a field you'd use sort of:
    onClick="document.formName.fieldName.value=arrayScaling(arrayName, 1)"
  6. Last but not least, the subroutine performs a backward or forward task also if the end of the array is reached, so that:
    1. If going forward the end of the array is reached, it will jump to its head and restart from there
    2. If going backward the first entry of the array is reached, it will jump to its tail and restart going backward from there
    In such fashion no warning will be issued if the tail or head of the array is reached. I find that most of the time an user doesn't want to be prompted when the end of an array is reached, but simply that the function is smart enough to guess it its own way and adapt itself without bothering the client.
  7. Possible invocations are:
    1. Forward:
      arrayScaling(arrayName, 1);
    2. Backward:
      arrayScaling(arrayName, 0);
For your convenience, I add here a snippet of two foo-foo arrays that you could use to build an onClick event wich fires an alert:
var testArray1=new Array("one","two","three","four");
var testArray2=new Array("five","six","seven","eight");

Tests are forward? check it »