PDA

View Full Version : Combining two javascripts



marringi
05-11-2008, 10:33 PM
Hello,

I have two scripts that are used with table id. I can only use one table id script at a time, so I am looking to combine the two scripts into one working script. Here are the scripts:

Script #1 (counts the amount of times a word appears in a table):

function rowCount(countWord){


var table = document.getElementById('tbr');
var rows = table.getElementsByTagName('tr');
var avoid = [0];// Separate numbers by commas. e.g. [0,5]

function inArray(arr,fval){
for(var i = 0; i < arr.length; i++){
if(arr[i] == fval)
return true;
}
return false;
}

var cellCount = new Array();
for(var i=0;i<rows.length;i++){
var cells = rows[i].getElementsByTagName('td');
for(var x=0;x<cells.length;x++){
var re = new RegExp(countWord,"gi");
if(cellCount[x] == null){cellCount[x] = 0;}
if(re.exec(cells[x].innerHTML) != null){
cellCount[x] += 1;
}
}
}

var totalsRow = document.createElement('tr');
for(var i=0;i<cellCount.length;i++){
var td = document.createElement('td');
if (!inArray(avoid,i))
td.innerHTML = cellCount[i];
totalsRow.appendChild(td);
}

table.tBodies[0].appendChild(totalsRow);

}

window.onload = function(){
var lidtvo;
if( lidtvo=window.location.search.match(/[?&]lidtvo=(\w+)/) )
var telja=lidtvo[1];
var divide=telja.replace(/(\S)([A-Z])/g,'$1 $2');
rowCount( divide ); //Pass the words which you want to count, seperate by pipe
}


Script #2 (displays separate colors on every two rows in a table)

function table(info)
{
var ele = document.getElementById(info);
for(x = 0; x < ele.rows.length; x++)
{
ele.rows[x].style.height = "20px"
if (x%2 == 0)
{
ele.rows[x].style.backgroundColor = "#FDFBED";
}
else
{
ele.rows[x].style.backgroundColor = "#F1EFE1";
}
}
}


Any ideas if this is possible and if so, how it could be performed?