2009/9/11 John J Barton <[email protected]>:
>
> Can someone help me understand what this Firebug method is doing?
>
> this.appendInnerHTML = function(element, html, referenceElement)
> {
> var doc = element.ownerDocument;
> var range = doc.createRange();
> range.selectNode(doc.body);
>
> var fragment = range.createContextualFragment(html);
> element.insertBefore(fragment, referenceElement);
> };
>
It looks like the first three lines are just setting the range to have
the contents of the body as selection - I think this is done because
the range has to have its start and end points specified to call most
of its methods.
Then the range's createContextualFragment method is used to parse a
string into a document fragment.
Finally, the document fragment is inserted before the
referenceElement, which must therefore be a child of element.
So if you had a section of the page like:
<p id="foo">Some text <span>in a span</span>.</p>
and called the function thus:
var foo = document.getElementById("foo");
var span = foo.getElementsByTagName("span")[0];
something.appendInnerHTML(foo, "<em>follows</em> ", span);
then you would end up with:
<p id="foo">Some text <em>follows</em> <span>in a span</span>.</p>
Regards,
Nick.
--
Nick Fitzsimons
http://www.nickfitz.co.uk/
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Firebug" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/firebug?hl=en
-~----------~----~----~----~------~----~------~--~---