Vincent Robert wrote:

> On Sep 24, 6:33 pm, Bertilo Wennergren <berti...@gmail.com> wrote:
>> I think I read somewhere that jQuery just passes the
>> code along to the "innerHTML" function of the browser.
>> That would mean that the actual rules are those of the
>> browsers' various implementations of "innerHTML" (whatever
>> those may be...).

Actually, jQuery does some parsing by itself first.

Yes, I had a look at the jQuery code. Fascinating stuff
in there.

The $('<span/>') syntax is actually a shortcut to
document.createElement("span"), and yes, the "/" is required here. So $
('<span/>') will be faster than $('<span></span>') which will have to
go through innerHtml.

A ha. Interesting. But, as it seems, if you use
".html('<span/>') or ".html('<span></span>'), they
amount to the same thing, as the first one gets
converted to the second one, and then gets passed
to "innerHTML".

Actually I'm a bit puzzled.
I tried the following six pieces of code:

1.
    var html = '<span/>';
    $("#div").html(html);

2.
    var html = '<span></span>';
    $("#div").html(html);

3.
    var html = $('<span/>');
    $("#div").html(html);

4.
    var html = $('<span></span>');
    $("#div").html(html);

5.
    $("#div").html($('<span/>'));

6.
    $("#div").html($('<span></span>'));

In all cases except 3 and 5 what happens is
that '<span></span>' finally gets passed through
innerHTML. So in 1 and 5 '<span/>' is actually
first turned into '<span></span>', and thus
it would (theoretically) have been more efficient
to write '<span></span>' in the first place.

I would have expected all six examples to
behave the same, but there are probably
good reasons for the current choices.

(I found out what happened using FireBug
and a few calls to "console.log()" inside
the jQuery code. This was all done in
Firefox, of course...)

--
Bertilo Wennergren <http://bertilow.com>

Reply via email to