PDA

View Full Version : Setting innerHTML decodes encoded characters


djBo
07-25-2008, 12:53 PM
Hi List,

My javascript AJAX class simply sets the innerHTML of a div to the result from the request.

The result contains a lot of HTML encoded characters using ASCII values like:

&#<value>;

When the innerHTML is set, the encoded characters are decoded to their literals (tested in IE&FF)

How can I precent this from happening?

Thanks in advance,
Rory

rnd me
07-25-2008, 01:01 PM
function escHT(s){
var o = new Option(s);
return o.innerHTML.replace(/amp;/g,"")
}

myDiv.innerHTML=escHT( xhr.responseText);

djBo
07-25-2008, 01:09 PM
rnd,

After testing this, it seems to works.
Until you SET the innerHTML of the div...
Then all the work we did in replacing the amp; is simply redecoded into literals *sighs*
Next to that, this doesn't seem to work in IE...

Rory

rnd me
07-26-2008, 02:07 AM
rnd,

After testing this, it seems to works.
Until you SET the innerHTML of the div...
Then all the work we did in replacing the amp; is simply redecoded into literals *sighs*
Next to that, this doesn't seem to work in IE...

Rory


doh!

sorry about that.

try this one (tested ie7, ff3)

function escHT(t){ //escape string (t) for HTML
var tt= document.createTextNode(t);
var d= document.createElement('div');
d.appendChild(tt);
return d.innerHTML;
}


you might also try removing the replace(...) on the first one.