...

View Full Version : How To Undo CreateElement?



dealmaker
08-17-2005, 01:44 AM
Hi,
I use createElement to create a bunch of elements, but I appendchild only the ones that fit certain condition. I have to createElement because I am testing certain fields inside the element.

But if I let the elements stay around without appendchild on them, will it create problem? I can't find a function like removeElement. Is there a function like that?

Many thanks.

SpirtOfGrandeur
08-17-2005, 02:27 AM
obj = object.appendChild( obj );

obj = object.removeChild( obj );

dealmaker
08-17-2005, 04:07 AM
yes, I know this. But what if I did createElement without appendChild, then I want to remove this element?

It seems that it's a waste of resource just to do appendChild and removeChild when I don't need to in the first place.


obj = object.appendChild( obj );

obj = object.removeChild( obj );

jkd
08-17-2005, 05:50 AM
Javascript does reference counting of some sort. I'm sure Liorean can explain it in depth, but essentially if you say:

var myElement = document.createElement("blabla");

Then never use myElement, the actual object returned by createElement only has that single reference, myElement, attached to it. When it goes out of scope, JS should know to garbage collect it for you.

So don't worry about it. :)

dealmaker
08-17-2005, 06:45 AM
So, can I use myElement = createElement("something") over and over again to create new elements in the same scope?

Now, it seems that there is another problem. I did a bunch of these createElement in the same function, and I assign image file names to the myElement.src.

Now the image files just won't showup in browser. If I reload the page, it shows. It seems that browser retrieve the image files and they are in cache, but they just won't show.

Do you know why?


Javascript does reference counting of some sort. I'm sure Liorean can explain it in depth, but essentially if you say:

var myElement = document.createElement("blabla");

Then never use myElement, the actual object returned by createElement only has that single reference, myElement, attached to it. When it goes out of scope, JS should know to garbage collect it for you.

So don't worry about it. :)

brothercake
08-17-2005, 07:40 PM
Because you haven't added it to the page - it's just in memory, not physically there.

Once you've created your element, you add it to the page, using appendChild, or insertBefore, or whatever method is appropriate.

createElement - creates an element, but does nothing with it
appendChild - adds an element to the page
removeChild - removes an element from the page

dealmaker
08-17-2005, 08:13 PM
No, I did as I stated in my original post.

Ok, this is a sample of what I do:



for(var i=0; i<5; i++) {

img = document.createElement('img');
img.src = imageFilesNames[i];

isAppend = false;

if(img.naturalWidth > 100 && img.naturalHeight > 100) {

img.style.width = 100 + 'px';
img.style.height = 100 + 'px';
isAppend = true;
}

if(isAppend) {
imgDiv.appendChild(img); // imgDiv is pointing to a DIV tag that already exist in html
}
}


It seems if I remove the naturalWidth/naturalHeight and the style assignment, the browser shows the images. If not, it doesn't show any and keep saying Waiting To Loading the images. If I appendChild before checking naturalWidth/naturalHeight and the style assignment, and removeChild after checking, the browser doesn't show any image because of removeChild. Same thing applies when I set img.style.visibility to 'hidden'.

Any idea why? I use debugger and verify that naturalWidth & naturalHeight already exist after I assign the img.src.


Because you haven't added it to the page - it's just in memory, not physically there.

Once you've created your element, you add it to the page, using appendChild, or insertBefore, or whatever method is appropriate.

createElement - creates an element, but does nothing with it
appendChild - adds an element to the page
removeChild - removes an element from the page

Erik-NA
08-17-2005, 09:34 PM
Javascript does reference counting of some sort. I'm sure Liorean can explain it in depth, but essentially if you say:

var myElement = document.createElement("blabla");

Then never use myElement, the actual object returned by createElement only has that single reference, myElement, attached to it. When it goes out of scope, JS should know to garbage collect it for you.

So don't worry about it. :)

It is possible to delete the object too



function deleteChildren (a_oNode) {
//make sure to remove all child nodes from the element
while (a_oNode.hasChildNodes()) {
deleteChildren (a_oNode.lastChild);
delete a_oNode.removeChild(a_oNode.lastChild);
}
}

deleteChildren (DomObject);
delete DomObject;

dealmaker
08-18-2005, 12:27 AM
I just figured it out, it turns out that I have to use onload on each image to make sure that it's "completely" loaded before I tried to read or write any of it's internal variables.

But the weird thing is that I can read all the internal variables of img element in the debugger without using onload.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum