Connecting Tech Pros Worldwide Help | Site Map

Ampersand creating problem in IE6 innerHTML property

 
LinkBack Thread Tools Search this Thread
  #1  
Old August 26th, 2008, 12:15 PM
Kiran Makam
Guest
 
Posts: n/a
Default Ampersand creating problem in IE6 innerHTML property

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

  #2  
Old August 26th, 2008, 12:15 PM
Martin Honnen
Guest
 
Posts: n/a
Default Re: Ampersand creating problem in IE6 innerHTML property

Kiran Makam wrote:
Quote:
div.innerHTML = "A&B";
Quote:
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/
  #3  
Old August 26th, 2008, 12:25 PM
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
Default Re: Ampersand creating problem in IE6 innerHTML property

Kiran Makam wrote:
Quote:
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>
Quote:
This is my code:
----------------
<body>
The DOCTYPE declaration and the required `title' element are missing.
Quote:
<div id='div1'></div>
<script>
The #REQUIRED `type' attribute is missing.
Quote:
var div = document.getElementById('div1');
You should do feature tests before you call anything.
Quote:
div.innerHTML = "A&B";
See below.
Quote:
</script>
>
</body>
---------------
>
IE6 renders the content of div1 as 'A'
That is correct.
Quote:
Firefox renders the content properly as 'A&B'
That is also correct.
Quote:
If there is a space after ampersand, IE6 renders it properly.
And that is correct as well.
Quote:
So I think that IE is assuming anything after ampersand as an HTML entity
( like &nbsp; ).
Which is standards compliant.
Quote:
Is this a bug in IE6?
No, the bug is in your code.
Quote:
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>
  #4  
Old August 26th, 2008, 12:25 PM
Bart Van der Donck
Guest
 
Posts: n/a
Default Re: Ampersand creating problem in IE6 innerHTML property

Kiran Makam wrote:
Quote:
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
  #5  
Old August 26th, 2008, 03:05 PM
Kiran Makam
Guest
 
Posts: n/a
Default Re: Ampersand creating problem in IE6 innerHTML property

Thanks to all.

Kiran Makam
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.