Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts

    Equivalent of insertAdjacentHTML in NS6?

    Does anyone know the equivalent of insertAdjacentHTML in NS6? I'm hoping this will work in early versions of NS6 as well.

    Thanks,
    - George
    - JavaScript Kit- JavaScript tutorials and 400+ scripts!
    - JavaScript Reference- JavaScript reference you can relate to.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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().

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

    Hope that helps!

    Happy coding!

  • #3
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    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:
    Code:
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •