JavaScript automatically converts some special characters

I need to extract the 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 is in the JS part, for example, when I try to extract a lö fragment that has a starting position depending on HTML, 3 and ending position 9 inside the <div> block. It seems that JS converts some special characters inside, so a counter from 3 to 9 mistakenly interpreted as " lösen " and not " l&ouml; ". Other special characters, such as &amp; do not affect this.

So my question is: if someone knows why JS behaves this way? Characters like &auml; or &ouml; are converted, and characters like &amp; or &nbsp; are equal. Is there any way to avoid this conversion?

I created a fiddle to demonstrate this: JSFiddle

Thanks for any help!

EDIT:

Perhaps I explained this a bit confusing, sorry for that. I want HTML:

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

Each special character must be uncured, except for HTML tags. Like in the HTML above.

But JS will automatically convert &ouml; or &uuml; in ö or ü what I need to avoid.

+7
source share
2 answers

This is because the browser (and not JavaScript) converts objects that do not need to be HTML escaped into the corresponding Unicode characters (for example, it skips &amp; &lt; and &gt; ).

So, by the time you check .innerHTML , it no longer contains what was in the original source of the page; you can cancel this process, but it includes a complete map of the <-> character pairs, which is simply not practical.

+2
source

If you understand correctly, try using innerHTML or .html ('your html code') for jQuery for the target element

0
source

All Articles