I am trying to replace text, but for this I need to loop through the text nodes of the div.
Each Div, when clicked, downloads the relevant content through ajax. But then I need to do a text replacement inside any of the text nodes inside.
My current code, after loading ajax content, iterates over all the text nodes of the entire page and is therefore too resource intensive.
I was looking for a clock, trying to figure out how to get around the loop through the div, and get the text nodes ...
and this should work in firefox, google chrome and ie6.
Any thoughts or suggestions?
As requested, here is the code:
function ajaxLoader(url, id) {
if (document.getElementById) {
var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
}
if (x) {
x.onreadystatechange = function () {
if (x.readyState == 4 && x.status == 200) {
el = document.getElementById(id);
el.innerHTML = x.responseText;
}
}
x.open("GET", url, true);
x.send(null);
}
CheckTranslate(id);
}
function CheckTranslate(id) {
var content = function (node, txt) {
if (txt) {
if (node.textContent) {
node.textContent = txt;
} else if (node.nodeValue) {
node.nodeValue = txt;
}
} else {
return node.textContent ? node.textContent : node.nodeValue;
}
};
$("#"+id).each(function() {
var obj = $(this).html();
var mylen = obj.length;
if (mylen > 0) {
}
});
(function (parent) {
var childs = parent.childNodes;
if (childs && childs.length) {
for (var i = 0, node; node = childs[i]; i++) {
if (node.nodeType == 3) {
var value = content(node);
var myclass = $(this).attr("class");
var ist = $(this).data('translated');
if (typeof(value) != 'undefined' && myclass != 'box_title' && ist != 'yes' && (myclass == 'status_bar' || myclass == '' || myclass == 'box_title_small' || myclass == 'status_bar_select')) {
for (var x = 0; x < en_count; x++) {
var from = en_lang[x];
var to = other_lang[x];
if (value.match(from)) {
content(node, value.replace(from, to));
if ($.browser.msie == 'false') {
$(node).data('translated', 'yes');
}
}
}
}
} else {
arguments.callee(node);
}
}
}
})(document.body);
}
There are 2 ajaxLoader functions that load the ajax contents for a div, and then CheckTranslate, which performs the translation after loading the new content.
, () , div.
, ...
, jquery ...