I need to make this jquery run much faster, I did not create all of these functions, but need their methods, to do live translations of page content.
So I would appreciate any suggestions to refactor for better performance, the code below.
/* function 1 read json into array */
/* function 2 loop through array */
/* function 3 replace text nodes based on data from looped array */
var en_lang = new Array();
var fr_lang = new Array();
var frca_lang = new Array();
var my_data = null;
var en_count = 1;
var fr_count = 1;
var frca_count = 1;
if(typeof(language) != "undefined"){
var language = 'frca';
}
function replaceText(oldText, newText, node){
node = node || document.body; // base node
var childs = node.childNodes, i = 0;
while(node = childs[i]){
if (node.nodeType == 3){ // text node found, do the replacement
if (node.textContent) {
node.textContent = node.textContent.replace(oldText, newText);
} else { // support to IE
node.nodeValue = node.nodeValue.replace(oldText, newText);
}
} else { // not a text mode, look forward
replaceText(oldText, newText, node);
}
i++;
}
}
function parsejSON(my_data) {
/* THIS PART GRABS DATA FROM TOP OF JSON FILE */
/* grab recordcount */
var recordcount = my_data.recordcount;
/* grab columnlist */
var columnlist = my_data.columnlist;
/* grab json data */
var json_data = my_data.data;
/* PUTS JSON DATA INTO ARRAYS */
for(var x = 0; x < recordcount; x++) {
var lng = json_data.lng[x];
var phrase = json_data.phrase[x];
if (lng == 'french') {
fr_lang[fr_count] = phrase;
fr_count = fr_count + 1;
}
if (lng == 'french canadian') {
frca_lang[frca_count] = phrase;
frca_count = frca_count + 1;
}
if (lng == 'english') {
en_lang[en_count] = phrase;
en_count = en_count + 1;
}
}
/* use a replacetext function above to replace all text */
for(var x = 0; x < en_lang.length; x++) {
var from = en_lang[x];
if (language == 'fr') {
var to = fr_lang[x];
}
if (language == 'frca') {
var to = frca_lang[x];
}
replaceText(from, to);
}
}
Sorry, not quite sure how to get the code posting, formatted correctly, feel free to edit my post in that situation..
Thank you.