Ampersand creating problem in IE6 innerHTML property

K

Kiran Makam

I am setting the content of a div dynamically using innerHTML
property. If the content contains an ampersand, text after the
ampersand is disappearing in IE6. It works properly in Firefox.

This is my code:
----------------
<body>

<div id='div1'></div>
<script>
var div = document.getElementById('div1');
div.innerHTML = "A&B";
</script>

</body>
---------------

IE6 renders the content of div1 as 'A'
Firefox renders the content properly as 'A&B'

If there is a space after ampersand, IE6 renders it properly. So I
think that IE is assuming anything after ampersand as an HTML entity
( like &nbsp; ).

Is this a bug in IE6? Is there any workaround for this?

Thanks
Kiran Makam
 
T

Thomas 'PointedEars' Lahn

Kiran said:
I am setting the content of a div dynamically using innerHTML
property. If the content contains an ampersand, text after the
ampersand is disappearing in IE6. It works properly in Firefox.

It works properly in both UAs, because there is no public standard that
defines how syntax errors MUST be handled. The HTML 4.01 Specification only
makes suggestions in that regard:

This is my code:

The DOCTYPE declaration and the required `title' element are missing.
<div id='div1'></div>
<script>

The #REQUIRED `type' attribute is missing.
var div = document.getElementById('div1');

You should do feature tests before you call anything.
div.innerHTML = "A&B";

See below.
</script>

</body>

That is correct.
Firefox renders the content properly as 'A&B'

That is also correct.
If there is a space after ampersand, IE6 renders it properly.

And that is correct as well.
So I think that IE is assuming anything after ampersand as an HTML entity
( like &nbsp; ).

Which is standards compliant.
Is this a bug in IE6?

No, the bug is in your code.
Is there any workaround for this?

Either

&amp;

or (better) using standards-compliant properties instead of the proprietary
`innerHTML' property.

Validate your code before you complain about browsers next time.

<http://validator.w3.org/>
<http://jibbering.com/faq/>


PointedEars
 
B

Bart Van der Donck

Kiran said:
var div = document.getElementById('div1');
div.innerHTML = "A&B";

The logic is that innerHTML considers the string as HTML; so '&...;'
should represent a valid numeric or character entity reference, while
(the avoidable) innerText considers it as the literal text.
 

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

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top