[Javascript] Strictly speaking (Was Javascript Detection)

Henrik Danielsson h.danielsson at gmail.com
Sat Jan 6 21:07:06 CST 2007


2007/1/3, Nick Fitzsimons <nick at nickfitz.co.uk>:
>
> On 3 Jan 2007, at 19:05:47, Matt Warden wrote:
>
> > On 1/3/07, Paul Novitski <paul at juniperwebcraft.com> wrote:
> >> At 1/3/2007 06:31 AM, Matt Warden wrote:
> >> >I find DOM manipulations to be elegant, once you wrap your ahead
> >> >around the fact that this is *not* markup anymore, but rather an
> >> >active document, represented by a tree structure.
> >>
> >> I've always considered document.write() to be sloppy, DOM-less
> >> coding, however the manual does say:
> >>
> >> "The text you write is parsed into the document's structure model. In
> >> the example above, the h1 element becomes a node in the document."
> >> http://developer.mozilla.org/en/docs/DOM:document.write
> >
> > I'm not sure how else it would work. I can't think through my head how
> > the markup itself could just be inserted as a text node but somehow
> > still function as part of the document.
> >
>
> I recently did a number of tests focused on this very issue (in
> relation to the WaSP's DSTF's invitation to assist the IE Team in
> prioritising DOM fixes for IE.next), and the conclusion I came to was
> that the string assigned to the innerHTML property is indeed passed
> through the HTML parser, at least on IE (and I assume on all
> browsers). I was able to prove this by triggering certain bugs in
> IE's parser when inserting supposedly-valid markup.
>
> One interesting side effect of this is that
>
> someElement.innerHTML = someMarkup;
> insertedMarkup = someElement.innerHTML;
> return insertedMarkup == someMarkup;
>
> doesn't necessarily return true; indeed, doing a more accurate
> comparison by walking the resultant DOM tree, allowing for things
> like capitalisation of tag names and attributes being unordered, will
> reveal that the DOM created by the parser won't necessarily be the
> same as if the equivalent DOM creation and manipulation methods are
> used.
>
> I'm still getting the results of the tests (which also cover assorted
> DOM oddities which modify the parser's behaviour on subsequent
> insertions of the same markup) written up, and will post a link when
> ready, for those who are interested.
>
> Regards,
>
> Nick.
> --
> Nick Fitzsimons
> http://www.nickfitz.co.uk/



Hello all,

I'm not a frequent writer on this list,
but I do read a lot of what's said and this got me interested.

I'm just wondering why there's ever been a doubt wether or not
HTML inserted via either document.write() or .inner/outerHTML
passes through the HTML parser? I thought that was obvious
(atleast for .innerHTML) since whatever you assign to it is
supposed to be interpreted as HTML, and what else than
the browser's HTML would be used for that?

And if it's parsed as HTML, why not insert it into the DOM tree?
Figuring out that it works is easy since whatever (valid) HTML
put in there is accessible via the DOM.

It's also likely that the dynamically generated DOM would not
be 100% equal to the DOM generated by static HTML since
the browser would "consider" different contexts when parsing
static vs inserted code. Some browsers also have a thing for
"correcting" code by adding/removing/editing tags to fit their
preferred profile, which is why reading back inserted code
would not return the original serialization, but a modified version
of it.

Please note that I have no insight whatsoever into the inner
workings of browsers, this is simply what I've observed
and concluded while developing web-based applications.

About the use of document.write() and .innerHTML...
I prefer to stay away from document.write() for various reasons,
but .innerHTML (and also outerHTML) does come in handy every now and then.
Sometimes I might simply use it's because it's faster (during runtime)
than the equivilent DOM methods, but that's a whole other discussion.
I think .innerHTML will be around for quite a while, and probably
document.write() too.
They are also good when you need to test something fast, but for more things
made
to be released, DOM is the future.

Regards,

Henrik
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.evolt.org/pipermail/javascript/attachments/20070107/dc303289/attachment.htm>


More information about the Javascript mailing list