...

Equivalent of insertAdjacentHTML in NS6?

WA
07-21-2002, 05:00 AM
Does anyone know the equivalent of insertAdjacentHTML in NS6? I'm hoping this will work in early versions of NS6 as well.

Thanks,

nolachrymose
07-21-2002, 01:22 PM
As far as I know there is none. I looked at the Geck DOM Reference Index, and the only function that resembled it (barely) is insertBefore() (http://www.mozilla.org/docs/dom/domref/dom_el_ref47.html#1019733).

IMO, you should just stick to creating elements dynamically and appending them to an element.

Hope that helps!

Happy coding! :)

jkd
07-21-2002, 10:49 PM
Think about what this requires:

1. Parsing a string into a DocumentFragment
2. Selectively inserting the DocumentFragment

For #1, Gecko provides 2 methods:

document.createRange().createContextualFragment('xml string');

And:

(new DOMParser()).parseFromString('xml string', 'text/xml')

The former is supported since very old Gecko builds though, so we'll use that. (Though in my trunk build from 6 days ago, it returns an error).

From that, you can use insertBefore() to insert the DocumentFragment.

Now, say the target element is target in Javascript.

To do a "beforeBegin":

target.parentNode.insertBefore(theDocFrag, target);

To do a "afterBegin":

target.insertBefore(theDocFrag, target.firstChild);

To do a "beforeEnd":

target.appendChild(theDocFrag);

And to do a "afterEnd":

target.parentNode.insertBefore(theDocFrag, target.nextSibling);

With all that in mind, you can through it all in one big HTMLElement.prototype function:

HTMLElement.prototype.insertAdjacentHTML = function(where, htmlstr) {
var docFrag = document.createRange().createContextualFragment(htmlstr);
switch (where) {
case 'beforeBegin':
this.parentNode.insertBefore(docFrag, this);
break;
case 'afterBegin':
this.insertBefore(docFrag, this.firstChild);
break;
case 'beforeEnd':
this.appendChild(docFrag);
break;
case 'afterEnd':
this.parentNode.insertBefore(docFrag, this.nextSibling);
break;
}
}


If I understand the MSDN docs correctly, that should work.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum