views:

66

answers:

3
+1  Q: 

IE HTML Escaping

I have a little problem that's driving me mad. I have the following example code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
<html>
    <script>
     function Test() {
      document.getElementById("test").innerHTML = "<input type='text' value='ab&apos;cef'>"
     }
    </script>
    <body>
     <div id="test"></div>
    </body>
    <script>
     Test();
    </script>
</html>

This example works great in FireFox 3.5, and I get to see an apostrophe in the middle of the text in the Input box. However, IE 7 displays it verbatim as: ap&apos;cef.

I've variously tried different escaping in the vain hope of seeing the apostrophe, for example \x27 or \' without any success. Does any clever person know how to do it in IE?

I realise I could use DOM methods to create nodes, but there's too much code for me to refactor it all.

Thanks!

+4  A: 

&apos; is an XML feature. It isn't allowed in HTML 4.x and isn't supported by Internet Explorer. Firefox supports it as part of its tag soup parser to cope with XHTML being served as text/html.

Use &#39; instead.

See also XHTML 1.0 Appendix C.

David Dorward
Works like a charm. I should go back to school and learn my HTML codes.
Jonathan Swift
A: 

(sorry, I misunderstood the question)

Skrim
Using setAttribute won't magically create support for ' in IE — and the OP specifically said they didn't want to refactor to use DOM methods.
David Dorward
A: 

document.getElementById("test").innerHTML = "<input type=\"text\" value=\"ab'cef\">"

Matthew Wilson
Thanks this works. But, doesn't work with double quotes!
Jonathan Swift