PDA

View Full Version : Dinamic Find and replace character using DOM, HOWTO?


sugar2
05-14-2005, 07:40 PM
(when you cant edit the source of the search results page, yahoo store webmasters knows what im talking about)

at thew moment of posting this new topic the search menu was disabled...

hi, look at this page: http://search.store.yahoo.com/cgi-bin/nsearch?catalog=yhst-16389103795725&query=quinceanera&image.x=24&image.y=14

it have nonhtml characters (like \ñ) and then cant be displayed, and as you can see, its a search results page...

in other words: a DOM way to dinamically find and replace the current "ñ" and replace they with a new "ñ".
and i ask for a DOM solution or starting point its because i cant edit the source of that page, and i cant edit the original html in wich contains the "ñ" character either.

do you have an idea about howto "Find and replace character using DOM?"

this is a pseudocode for the solution im asking for: (and i said pseudocode because i donnt know if we have a getElementsByText or a document.createText, im just Speculating:
i dont know is it can be doable... please giveme ideas or please helpme making that code doable and actually working


function changeLatinN(){
if(!document.getElementsByText) return;
var d, h=document.getElementsByText('ñ');
while(h.length){
(d=document.createText('ñ'));
h[0].parentNode.replaceChild(d,h[0]);
}
}


thanks

enumerator
05-16-2005, 04:37 AM
The characters show up in my browser, but um... something like this may work:

document.body.innerHTML = document.body.innerHTML.replace(/ñ/g,"ñ");

The DOM has no equivalent to the innerHTML property, or a getElementsByText method, but you may be able to replace all text nodes, otherwise...

hemebond
05-16-2005, 05:30 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>59082</title>
</head>
<body>
<p>ñ</p>

<script type="text/javascript">
var txt = document.getElementsByTagName('p')[0].childNodes[0];
var textNodes = new Array();

walk(document.getElementsByTagName('body')[0]);
replaceCharacter();

function walk(node)
{
if(node.childNodes.length > 0)
{
for(var i = 0; i < node.childNodes.length; i++)
{
walk(node.childNodes[i]);
}
}

if(node.nodeName == '#text')
{
textNodes.push(node);
}
}

function replaceCharacter()
{
for(var i = 0; i < textNodes.length; i++)
{
textNodes[i].nodeValue = textNodes[i].nodeValue.replace('ñ', '\u00f1');
}
}
</script>
</body>
</html>

sugar2
05-16-2005, 06:46 PM
GREAT!

it works!!!
thanks,.. i will try to implement on my yahoo store page...!

thanks :thumbsup: