By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,297 Members | 1,429 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,297 IT Pros & Developers. It's quick & easy.

Ampersand creating problem in IE6 innerHTML property

P: n/a
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
Aug 26 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Kiran Makam wrote:
div.innerHTML = "A&B";
Is this a bug in IE6? Is there any workaround for this?
No, it is not a bug, the ampersand needs to be escaped as &amp;
(& a m p ; without the blanks) e.g.
div.innerHTML = "A&amp;B"

--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 26 '08 #2

P: n/a
Kiran Makam wrote:
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:

<http://www.w3.org/TR/1999/REC-html401-19991224/appendix/notes.html#h-B.1>
This is my code:
----------------
<body>
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>
---------------

IE6 renders the content of div1 as 'A'
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
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Aug 26 '08 #3

P: n/a
Kiran Makam wrote:
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.

--
Bart
Aug 26 '08 #4

P: n/a
Thanks to all.

Kiran Makam
Aug 26 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.