I wrote this script to split an array, sort each individual array, and then access them both into a select. So as an example, it would split the below array into numbers and letters and then display the numbers in numerical order and also attach the letter next to the number. For some silly reason this works in every browser EXCEPT Safari. From the testing I've been able to do, Safari doesn't access the part where I've bolded in the text. I realize I'm new to posting here, but I think this is actually somewhat interesting (though frustrating)
The overall biggest issue is that the values of code_sorted remain undefined when creating the select in Safari.
Thanks in advance ...
Code:
var temp = new Array(); //temp Array for holding strings
var code = new Array(); //holds client codes
var name = new Array(); //holds client name
var pass = "on"; //sorts the split string to an appropriate case
var name_count = 0; //keeps array count for name
var code_count = 0; //keeps array count for code
var name_sorted = new Array(); //holds final alpha_name_array
var code_sorted = new Array(); //holds final code array in relation to alpha
// "clientCODE|clientNAME|",
var client_codes = [
"A|1|",
"B|12|",
"C|43|",
"D|24|",
"E|5|",
"F|66|",
"G|37|",
"H|28|",
"I|9|",
"J|18|"
];
//JS Functions.
function joinArray(client) //Convert array to string to split.
{
b = client.join("");
return(b);
}
function splitclients(client) //splits string and puts back into temporary array.
{
joinArray(client)
temp = b.split('|');
}
//creates select with name 'clcode' and populates with data from client array
function create_select(name,code, w) //Also accepts width from HTML
{
document.write('<select name = "s_clcode" onChange="upSel();" id = "s_clcode" class = "ml" style = "width:'+w+'px;"><option value = "0">Please Select:</option>');
for(var i = 0; i < name.length; i++)
{
document.write('<option value = "'+code[i]+'">'+name[i]+' - '+code[i]+'</option>');
}
document.write('</select>');
}
function sort_them(temp) //sorts temporary data into either code or name arrays
{
for(var i = 0; i < temp.length-1; i++)
{
if(pass == "on")
{
code[code_count] = temp[i];
pass = "off";
code_count++;
}
else{
name[name_count] = temp[i];
name_sorted[name_count] = temp[i];
pass = "on";
name_count++;
}
}
}
function alpha_sort(name_sorted, name, code) //sorts the data alphabetically for display and matches code to alpha_sorted name
{
name_sorted.sort();
for(var x = 0; x < name_sorted.length; x++)
{
for(var l = 0; l < name_sorted.length; l++)
{
if(name_sorted[x] == name[l])
{
code_sorted[x] = code[l];
}
}
}
}
function clcoder(client, width) //clean protocol, this is the ONLY code that needs called in the HTML
{
splitclients(client);
sort_them(temp);
alpha_sort(name_sorted, name, code);
create_select(name_sorted, code_sorted, width);
}
Last edited by Fang; 05-22-2010 at 01:37 AM.
Reason: added BB Code
Bookmarks