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 9 of 9
  1. #1
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts

    insertAdjacentElement equivalent

    IE has a nifty method called insertAdjacentElement with which you can say things like "move this object before that object" or "move that object between these two objects", just like that. Very useful.

    So, how can I do the same thing with non-proprietary methods?

  • #2
    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
    Sure, just use appendChild(), or insertBefore(), like so:

    Code:
    HTMLElement.protoype.insertAdjacentElement = function(where, element) {
    	switch (where.toLowerCase()) {
    		case "beforebegin":
    			this.parentNode.insertBefore(element, this);
    			break;
    		case "afterbegin":
    			this.insertBefore(element, this.firstChild);
    			break;
    		case "beforeend":
    			this.appendChild(element);
    			break;
    		case "afterend":
    			this.parentNode.insertBefore(element, this.nextSibling);
    			break;
    	}
    }

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    excellent




    <aftershock>
    Hey ... I just realised ... you used prototype to make gecko recognise an IE-proprietary method name ...

    ... invent any methods with any behavior and any name ... anything in fact ....

    Dagnammit! I was impressed before; but this element-prototyping is so so cool
    </aftershock>
    Last edited by brothercake; 03-05-2003 at 10:06 PM.

  • #4
    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
    http://www.webfx.eae.net/dhtml/ieemu/

    It gets really cool when to put setters and getters onto prototyped elements, like

    HTMLElement.prototype.__defineGetter__("outerHTML", function() {
    return (new XMLSerializer()).serializeToString(this);
    });


  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts

    Memory leaks

    From my experience, you shouldn't go add prototype methods or getters/setters on DOM objects - they aren't garbage collected correctly unless you close down mozilla - meaning the entire application, not only the page or even all open browser windows.

    On the other hand, string concatenation using += also leaks (but far less), so in comparison that might not be that much of an issue. Besides, memory is cheap these days.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    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

    Re: Memory leaks

    Originally posted by liorean
    From my experience, you shouldn't go add prototype methods or getters/setters on DOM objects - they aren't garbage collected correctly unless you close down mozilla - meaning the entire application, not only the page or even all open browser windows.
    Have a bug number?

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What is a memory leak anyway? I hear it bandied about, but I've no idea what it means.

  • #8
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    A memory leak is when the browser doesn't return the memory for an object whose refcount has gone down to zero, or that the refcount actually never reaches zero and therefore doesn't garbage collect the object. For IE this often means not as long as you don't restart your computer, while for mozilla, it's often only until page reload or you close down the application. In the few cases I've seen it happen in Opera, it's been reclaimed as soon as you start the browser again. This meant I sometimes got a 300MB extra memory comsumption as soon as I started Opera. (7 beta 1 did this, I've never seen it happen in other versions)

    JDK: No one single, but there's several covering it. There seems to have been a number of different things that all act together on DOM objects. (most were fixed before 1.0, by the way)
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #9
    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
    Originally posted by liorean
    JDK: No one single, but there's several covering it. There seems to have been a number of different things that all act together on DOM objects. (most were fixed before 1.0, by the way)
    Not even a tracker bug? (I'm hesitant to search through Bugzilla on a wild goose chase). Also, if most were fixed before 1.0, then why worry? 1.0 is the lowest version of Mozilla you should ever keep in mind while developing.


  •  

    Posting Permissions

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