...

View Full Version : simple onload javascript problem



maxpouliot
05-24-2006, 03:42 PM
I need to load an array when my page loads which im doint by calling the arrayEmploye() function


function arrayEmploye()
{
var arrayEmp=[];
var selobj2=document.getElementById('employe');
for (var zxc0=0;zxc0<selobj2.options.length;zxc0++)
{
arrayEmp.push([zxc0,selobj2.options[zxc0].text]); // Note this line
}
return arrayEmp;
//alert(arrayEmploye);
}

After that, in another function, i need to have the array (arrayEmp) available.


function Select(obj,selid)
{
alert(arrayEmp);
var txt=obj.value;
var selobj=document.getElementById(selid);
var ary=[];

for (var zxc0=0;zxc0<selobj.options.length;zxc0++)
{
ary.push([zxc0,selobj.options[zxc0].text]); // Note this line
}


//ary=ary.sort(Sort);
selobj.selectedIndex=0;
for (var zxc1=0;zxc1<ary.length;zxc1++)
{
if (txt.toLowerCase()==ary[zxc1][1].substring(0,txt.length).toLowerCase())
{ // Note this line checks text in field 1
selobj.selectedIndex=ary[zxc1][0]; // Note this line index in field 0
break;
}
}
}

How can i do this?

Kor
05-24-2006, 04:50 PM
simply call the function onload:

onload=arrayEmploye;

maxpouliot
05-24-2006, 04:59 PM
simply call the function onload:

onload=arrayEmploye;

This is what i'm trying to avoid... because my arrayEmploye function is long to run, i would like to run it once, store it somewhere and access it whenever i want to later on... Do you understand.

Kor
05-24-2006, 05:03 PM
Sound tricky... You should run it sometime, and when running will take the same long time...

Well, you may run it as first line in your second function

function Select(obj,selid)
{
var arrayEmp= arrayEmploye();
..the rest of the code
}

maxpouliot
05-24-2006, 05:09 PM
Sound tricky... You should run it sometime, and when running will take the same long time...

Well, you may run it as first line in your second function

function Select(obj,selid)
{
var arrayEmp= arrayEmploye();
..the rest of the code
}

is there a way to call the arrayEmploye() function in the Select function the first time and then store the value. Then, the second time i call the Select function, it doesn't call the arrayEmploye function?

Kor
05-24-2006, 05:26 PM
Make the array a global variable (push it outside the function)

var arrayEmp=[];
function arrayEmploye()
{
var selobj2=document.getElementById('employe');
for (var zxc0=0;zxc0<selobj2.options.length;zxc0++)
{
arrayEmp.push([zxc0,selobj2.options[zxc0].text]); // Note this line
}

}
....



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum