outerHTML decoded special caracteres

L

louvinon

Hi,

I use the function outerHTML in order to get the html of a div.
But specials caracteres are decoded.

For example :
when I click on the button "Show" , I obtain :

<DIV id=example val='test with &amp; $B!l(B < " > '>test with &amp; &lt; "
&gt;</DIV>

If you compare the html code (see below) with the value of the
attribute 'val', you can see it's different. &lt; becomes <, &gt;
becomes > ...
How can I do to get exactly the same code ?

Thanks.

Code:
<html><head>
<title>SOS</title>
<script>
function show() {
alert(document.getElementById("example").outerHTML);
}
</script>
</head><body>
<div id="example" val="test with &amp; &prime; &lt; &quot; &gt;
">test with &amp; &lt; &quot; &gt;</div>
<button onclick="show()">SHOW</button>

</body>
</html>
 
E

Evertjan.

wrote on 23 jan 2009 in comp.lang.javascript:
I use the function outerHTML in order to get the html of a div.
But specials caracteres are decoded.

For example :
when I click on the button "Show" , I obtain :

<DIV id=example val='test with &amp; $B!l(B < " > '>test with &amp;
&lt; " &gt;</DIV>

If you compare the html code (see below) with the value of the
attribute 'val', you can see it's different. &lt; becomes <, &gt;
becomes > ...
How can I do to get exactly the same code ?

You cannot,
as the sourcecode is not available to the DOM,
and only the DOM is the source of the outerHTML.
 
T

Thomas 'PointedEars' Lahn

I use the function outerHTML in order to get the html of a div.
But specials caracteres are decoded.

No, mostly they are read as they should be in the document tree. HTML
parsing makes `&' out of `&amp;', `<' out of `&lt;' aso. in the document
tree. The `innerHTML' and `outerHTML' properties are supposed to be
serializations of (parts of) that data structure.
For example :
when I click on the button "Show" , I obtain :

<DIV id=example val='test with &amp; ′ < " > '>test with &amp; &lt; "
&gt;</DIV>

Looks like a combination of a bug and a font issue. `&amp;' should have
been `&', and ′ is probably not display properly because the font used for
window.alert() does not provide this character. Or, it is an encoding
issue (see below).
If you compare the html code (see below) with the value of the
attribute 'val', you can see it's different. &lt; becomes <, &gt;
becomes > ...
How can I do to get exactly the same code ?

Don't use outerHTML (or innerHTML, for that matter).
Code:
<html><head>[/QUOTE]

The declaration of the default encoding as per the
`meta[http-equiv="Content-Type"]' element is missing.  If the Content-Type
HTTP header is also missing or if it lacks a `charset' parameter, the
document output is the result of browser's guesswork.  Always declare your
encoding.

[QUOTE]
<title>SOS</title>
<script>[/QUOTE]

The required `type' attribute is missing.
[QUOTE]
function show() {
alert(document.getElementById("example").outerHTML); 
window.alert(...);

}
</script>
</head><body>
<div id="example" val="test with &amp; &prime; &lt; &quot; &gt;
">test with &amp; &lt; &quot; &gt;</div>[/QUOTE]

DIV elements don't have a `val' attribute.  Your markup is not Valid, so you
should not expect anything.

[QUOTE]
<button onclick="show()">SHOW</button>[/QUOTE]

<input type="button" value="SHOW" onclick="show()">

suffices here, although precautions should be taken for user agents that do
not support client-side scripting.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,619
Members
45,316
Latest member
naturesElixirCBDGummies

Latest Threads

Top