1996 Copyright by Stefan Koch. All rights reserved.
Working with arrays One important programming technique is the use of arrays. Most programming language support them in a certain manner. Maybe you have already come to a point where arrays would help you further. But if you looked through the documentation provided by Netscape you realized that nothing is said about arrays. The problem is that arrays do not eist in !ava"cript# But $ can show you a work around of course# %irst& what are arrays' (ou could say that arrays are many variables bound together. )et*s say that you want +, variables. (ou could begin with the variables a& b& c ... But this is really complicated. -specially if you want to store +,, or more variables. $f you have an array which is called *My.rray* and it has got +, elements& you could address the different elements with My.rray/+0& My.rray/10& My.rray/20... 3many programming languages start with the , as the first element4 but we want to have the + as first element because this is the way we use it later in !ava"cript5. "o if you want to keep the number +6 in the first element you have to write My.rray/+07+6. (ou see that you can work with arrays the same way as with normal variables. But there are some more features. $f you want to store the number +6 in every element you can write it this way8 for 3var i7+9i:++9i;;5 My.rray/i07+6 The for4 command tells the computer how often the net command is going to be eecuted. The for4 loop starts with i7 +. %irst the computer gets the command My.rray/+07 +6. .fter this i is incremented by + and then the command is My.rray/107 +6. i is incremented until it has the value +,. 3The epression i:++ in the for loop has to be true4 if i is ++ this epression isn*t *true* anymore5. Now $ will show you a function to initialize arrays. This function was discussed in the !ava"cript Mailing list. function init.rray35 < this.length 7 init.rray.arguments.length for 3var i 7 ,9 i : this.length9 i;;5 this/i;+0 7 init.rray.arguments/i0 = (ou don*t need to understand this function. (ou only have to know how to use this function. $f you want to create an array with three elements you >ust write8 var My.rray7 new init.rray3+6&+?&+@5. +6&+?&+@ are assigned to the elements +&1&2. (ou could as well write strings to your array. "o you don*t have to matter which type is used# var heyho7 new init.rray3AThisA&AisA&AcoolA5. Miing different types is no problem at all8 var Miedup7 new init.rray3+6&AyoA&+,25. Bere is the output of the following code8 Element No. 1: 17 C 4 + Element No. 2: yo Element No. 3: 103 This is the script for the output8 :script language7A!ava"criptAD :#44 Bide var Miedup7 new init.rray3+6&AyoA&+,259 document.write3A-lement No. +8 A;Miedup/+0;A:brDA59 document.write3A-lement No. 18 A;Miedup/10;A:brDA59 document.write3A-lement No. 28 A;Miedup/20;A:brDA59 EE 44D :EscriptD Clearing a window ot frae $ have written a small game yesterday. $ have encountered a problem you might have got as well. $f you want to clear a window or frame you look into the documentation by Netscape and see that !ava"cript knows the function *document.clear35*. But if you implement this function nothing happens# The function document.clear35 seems to be broken on every platform. Fordon McGomb gave me the following script which clears the window as $ wanted it. document.close359 document.open359 document.write3A:HDA59 (ou don*t have to write document.write3A:HDA59. $t is only important that you send anything to the window. This works fine with frames as well. !istory" f#nctions$ Creating %back% and %forward% with &avaScript Now we*re looking at a script which lets you navigate through different documents. Ihat $*m talking about is the back35 and forward354 function. $f you have a back4 link on your page this isn*t the same as the back4 button in the Netscape4 Navigator. %or eample $ have got some back4 links which work like normal links but $ know that the user probably comes from that certain page $*m linking to. The back4 button provided by the Netscape Navigator goes one step back in your history list. (ou can do this with !ava"cript as well. !ust take this link in order to get back again# The script $ used here is shown below8 :htmlD :bodyD :%OJM N.M-7AbuttonbarAD :$NHKT T(H-7AbuttonA L.)K-7ABackA onGlick7Ahistory.back35AD :$NHKT T(H-7AbuttonA L.)K-7A!"4 BomeA onGlick7Alocation7*script.htm*AD C 4 1 :$NHKT T(H-7AbuttonA L.)K-7ANetA onG)ick7Ahistory.forward35AD :E%OJMD :EbodyD :EhtmlD (ou could also write history.go34+5 and history.go3+5. Last changed: 11.May'96 1996 by Stean !och C 4 2