logo

New Response

« Return to the blog entry

You are replying to:

    • avatar
    • Sam
    • Posted on Wed 28 Apr 2004 15:27

    FYI, here's a JavaScript constructor I wrote to use insertAdjacentHTML() for Netscape browsers. (I have a constructor for innerHTML() at home, so I'll post it when I can find it.) Enjoy!

    ====================================

    if (document.childNodes&&!document.childNodes[0].insertAdjacentHTML){

    HTMLElement.prototype.insertAdjacentHTML = function (sWhere, sHTML) {

    var df;

    var r = this.ownerDocument.createRange();

    switch (String(sWhere).toLowerCase()) {

    case "beforebegin":

    r.setStartBefore(this);

    df = r.createContextualFragment(sHTML);

    this.parentNode.insertBefore(df, this);

    break;

    case "afterbegin":

    r.selectNodeContents(this);

    r.collapse(true);

    df = r.createContextualFragment(sHTML);

    this.insertBefore(df, this.firstChild);

    break;

    case "beforeend":

    r.selectNodeContents(this);

    r.collapse(false);

    df = r.createContextualFragment(sHTML);

    this.appendChild(df);

    break;

    case "afterend":

    r.setStartAfter(this);

    df = r.createContextualFragment(sHTML);

    this.parentNode.insertBefore(df, this.nextSibling);

    break;

    }

    }

    }

Your Comments

Name:
E-mail:
(optional)
Website:
(optional)
Comment: