Arrays

Compiling Data
Passing Data
Name & Value
Muliple Elements
Variable & Value
 
Data to Popup
Data from Popup
Opener
Arrays
Form Method
 

Single Array

Page1.htm

<script type="text/javascript">
<!--
sample=new Array()
sample[0]="data1"
sample[1]="info2"
sample[2]="hello world"

function sendData(){
str=""
for(var i=0;i<sample.length;i++){
str+=sample[i]

if(i<sample.length-1){
str+="&"
}

}

location.href="page2.htm?"+str

}
//-->
</script>

Page2.htm

<script type="text/javascript">
<!--
function chkData(){
if (location.search.length > 0){
sample=unescape(location.search.substring(1)).split("&")

document.getElementById("display").innerHTML=sample+"<br><br>"+sample[0]+"<br>"+sample[1]+"<br>"+sample[2]
}
}

// add onload="chkData()" to the opening BODY tag

//-->
</script>

<div id="display"></div>


Two dimensional array

Because data is transfered as a single string a two-dimensional array will lose its format.
In order to be able to reconstruct a two-dimensional array a delimiter is used to separate each index value of the sample array and a second delimiter to separate each index value of the sub arrays.

In order to reconstruct our array we need to use some tempory arrays to process the string.

The string is first split to get the collective values for each sub array then these values are split to regain each sub arrays values.


Page1.htm

<script type="text/javascript">
<!--
sample=new Array()
sample[0]=new Array("data1","data2","data3","data4")
sample[1]=new Array("info1","info2")
sample[2]=new Array("hello","world")

function sendData(){ str="" for(i=0;i<sample.length;i++){ for(j=0;j<sample[i].length;j++){ str+=sample[i][j] if(j<sample[i].length-1){ str+="=" // add delimiter } } if(i<sample.length-1){ str+="&" // add delimiter } } location.href="page2.htm?"+str } //--> </script>

The above function gives you

Notice that each index value of the sample array is seperated by & and each index value of the sub array is seperated by =

Page2.htm

<script type="text/javascript">
<!--
function chkData(){
if (location.search.length > 0){

newSample=unescape(location.search.substring(1)).split("&")

for(k=0;k<newSample.length;k++){
tempArray2=newSample[k].split("=")

newSample[k]=new Array()

for(l=0;l<tempArray2.length;l++){
newSample[k][l]=tempArray2[l]
}

}

}

// Examples for referencing the array indexes

indexValue=newSample[0]+"<br>"+newSample[1]+"<br>"+newSample[2]

indexValue+=newSample[0][0]+"<br>"
indexValue+=newSample[0][1]+"<br>"
indexValue+=newSample[0][2]+"<br>"
indexValue+=newSample[0][3]+"<br>"

indexValue+=newSample[1][0]+"<br>"
indexValue+=newSample[1][1]+"<br>"

indexValue+=newSample[2][0]+"<br>"
indexValue+=newSample[2][1]+"<br>"

document.getElementById("display").innerHTML=indexValue

}

// add onload="chkData()" to the opening BODY tag 

//-->
</script>

<div id="display"></div>