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?
On Feb 17, 3:20 pm, Yansky <thegoodd...@gmail.com> wrote:
> 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?
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
> On Feb 17, 3:20 pm, Yansky <thegoodd...@gmail.com> wrote:
> > 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?
> 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
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);
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?
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));