views:

1154

answers:

2

IE does not work good with unknown elements (ie. HTML5 elements), one cannot style them , or access most of their props. Their are numerous work arounds for this for example: http://remysharp.com/2009/01/07/html5-enabling-script/

The problem is that this works great for static HTML that was available on page load, but when one creates HTML5 elements afterward (for example AJAX call containing them, or simply creating with JS), it will mark these newly added elements them as HTMLUnknownElement as supposed to HTMLGenericElement (in IE debugger).

Does anybody know a work around for that, so that newly added elements will be recognized/enabled by IE?

Here is a test page:

<html><head><title>TIME TEST</title>
    <!--[if IE]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"&gt;&lt;/script&gt;
<![endif]-->
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
</head>

<body>
    <time>some time</time>
    <hr>
    <script type="text/javascript">
        $("time").text("WORKS GREAT");
        $("body").append("<time>NEW ELEMENT</time>"); //simulates AJAX callback insertion
        $("time").text("UPDATE");
    </script>
</body>
</html>

In IE you will see the: UPDATE , and NEW ELEMENT. In any other modern browser you will see UPDATE, and UPDATE


Solution

Using the answer provided I came up with the following piece of javascript to HTML5 enable a whole bunch of elements returned by my ajax call:

(function ($) {
    jQuery.fn.html5Enable = function () {
        if ($.browser.msie) {
            $("abbr, article, aside, audio, canvas, details, figcaption, figure, footer, header, hgroup, mark, menu, meter, nav, output, progress, section, summary, time, video", this).replaceWith(function () {
                if (this.tagName == undefined)
                    return "";
                var el = $(document.createElement(this.tagName));
                for (var i = 0; i < this.attributes.length; i++)
                    el.attr(this.attributes[i].nodeName, this.attributes[i].nodeValue);

                el.html(this.innerHtml);

                return el;
            });
        }


        return this;

    };
})(jQuery);

Now this can be called whenever you want to append something:

var el = $(AJAX_RESULT_OR_HTML_STRING);
el.html5Enable();
$("SOMECONTAINER").append(el);

See http://code.google.com/p/html5shiv/issues/detail?id=4 for an explanation about what this plugin doesn't do.

Updated Solution

Use innershiv , it's great.

+4  A: 

jQuery has some dark, magical ways of creating elements. Using document.createElement instead should make all the difference:

var time = document.createElement("time");
time.innerHTML = "WORKS GREAT";
document.appendChild(time);

I do not believe you can use the .append() syntax (like .innerHTML += "") with HTML5 and IE. The problem isn't IE's ability to use or display the HTML5 elements, it's its ability to parse them. Any time you programmatically instantiate an HTML5 element, you must do it with document.createElement.

mattbasta
I saw this works, but the page above was just an example, the real problem lays in the fact that these elements are loaded by ajax: $("#el").load("url"). So here your workaround doesn't apply.
Gidon
@Gidon: but again, you’re using jQuery to do your AJAX call there, right? jQuery seems to be the problem here, not AJAX. (Disclaimer: I may well be missing the point entirely.)
Paul D. Waite
@Gidon: Perhaps you can create the elements and just stick the values into them (as opposed to creating the elements by passing it in with jQuery). jQuery isn't IE-HTML5 safe, so you'll just have to load it in in that manner. Otherwise, just stick to XHTML.
mattbasta
This was indeed the only solution, yet since the ajax call return a whole set of html I had to parse it first, since document.createElement works only with one element at a time, and not a collection. I added my code in my answer.
Gidon
+3  A: 

hi,

for all html5 issues in IE7 i use html5shiv and to accommodate the html5 elements coming back in ajax calls i use innershiv.

these two small plugins worked for me like a charm so far.

I cant submit both URLs still. because i dont have 10 reputation points :(

-- Praveen Gunasekara

Praveen