JavaScript automatically converts some special characters

Posted by noplacetoh1de on Stack Overflow See other posts from Stack Overflow or by noplacetoh1de
Published on 2012-11-22T13:56:17Z Indexed on 2012/11/23 17:05 UTC
Read the original article Hit count: 209

I need to extract a HTML-Substring with JS which is position dependent. I store special characters HTML-encoded.

For example:

HTML

<div id="test"><p>l&ouml;sen &amp; gr&uuml;&szlig;en</p></div>?

Text

lösen & grüßen

My problem lies in the JS-part, for example when I try to extract the fragment , which has the HTML-dependent starting position of 3 and the end position of 9 inside the <div> block. JS seems to convert some special characters internally so that the count from 3 to 9 is wrongly interpreted as "lösen " and not "l&ouml;". Other special characters like the &amp; are not affected by this.

So my question is, if someone knows why JS is behaving in that way? Characters like &auml; or &ouml; are being converted while characters like &amp; or &nbsp; are plain. Is there any possibility to avoid this conversion?

I've set up a fiddle to demonstrate this: JSFiddle

Thanks for any help!

EDIT:

Maybe I've explained it a bit confusing, sorry for that. What I want is the HTML:

<p>l&ouml;sen &amp; gr&uuml;&szlig;en</p> .

Every special character should be unconverted, except the HTML-Tags. Like in the HTML above.

But JS converts the &ouml; or &uuml; into ö or ü automatically, what I need to avoid.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery