Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

createContextualFragment

20 views
Skip to first unread message

Yansky

unread,
Feb 16, 2008, 9:20:32 PM2/16/08
to
I posted this over at netscape.public.mozilla.dom but the group seems
to be dead, so I figured I'd repost my question here:

I was just wondering why createContextualFragment is available in
Mozilla browsers when they also have innerHTML which seems to do
exactly the same thing?

http://developer.mozilla.org/en/docs/DOM:range.createContextualFragment

Cheers.

Paul Wilkins

unread,
Feb 16, 2008, 11:43:28 PM2/16/08
to

Sometimes IE wins, as with innerHTML vs createContextualFragment
Sometimes IE loses, as with document.all s document.getElementById
Sometimes two competing ideas don't win out, as in addEventListener
and attachEvent

--
Paul Wilkins

dhtml

unread,
Feb 17, 2008, 12:36:46 AM2/17/08
to
Can you do dynamic script insertion with that?

var s = "<script>alert(1)</script>";
var range = document.createRange();
range.selectNode(document.body);
var documentFragment = range.createContextualFragment(s);
document.body.appendChild(documentFragment);

It looks like you can.

> --
> Paul Wilkins

Martin Honnen

unread,
Feb 17, 2008, 9:13:07 AM2/17/08
to
Yansky wrote:

> I was just wondering why createContextualFragment is available in
> Mozilla browsers when they also have innerHTML which seems to do
> exactly the same thing?
>
> http://developer.mozilla.org/en/docs/DOM:range.createContextualFragment

createContextualFragment in Mozilla was first, then innerHTML support
was implemented with the help of createContextualFragment.
createContextualFragment by now works for XML documents too where you do
not have innerHTML e.g.

var xmlDoc = new DOMParser().parseFromString(
'<root><foo/></root>', 'application/xml');
var node = xmlDoc.documentElement.getElementsByTagName('foo')[0];
var range = xmlDoc.createRange();
range.selectNode(node);
var fragment = range.createContextualFragment(
'<bar>foobar</bar><!-- comment -->');
node.appendChild(fragment);
alert(new XMLSerializer().serializeToString(xmlDoc));


--

Martin Honnen
http://JavaScript.FAQTs.com/

0 new messages