[Javascript] Changing an Objects ID and Name

Paul Novitski paul at juniperwebcraft.com
Thu Sep 7 05:33:35 CDT 2006


At 9/6/2006 11:07 PM, Jonathan Buchanan wrote:
>On 9/7/06, Terry Riegel <riegel at clearimageonline.com> wrote:
>>
>>I modified it slightly to include the type of node being swapped.
>>
>>function swapNodes(n1, n2, k)
>>{
>>     var placeHolder = document.createElement(k);
>>     n1.parentNode.replaceChild(placeHolder, n1);
>>     n2.parentNode.replaceChild(n1, n2);
>>     placeHolder.parentNode.replaceChild(n2, placeHolder);
>>}
>>
>>Is there some type of cleanup needed to remove the temporary node
>>(placeHolder)? Or is it local to the function and gets removed
>>automatically?
>
>As placeHolder is declared using "var", it's local to the function.
>When the function returns, placeHolder is not attached to the document
>any more and there are no other references to it hanging around, so it
>should get removed automatically.


Jonathan, I suspect you're wrong on this point.  While I agree that 
the variable 'placeHolder' is local to the function 'swapNodes()' and 
will be cleared when the function terminates, surely when you invoke 
the DOM method createElement() you're creating a new element IN THE 
DOM which has got to be about as global as you can get in a script 
running in the context of the Document Object Model.

Am I wrong about this?  Does the createElement() method return a node 
object that isn't actually part of the DOM and doesn't persist when 
the calling function returns?

Regards,
Paul 




More information about the Javascript mailing list