D
Dom Bannon
Sorry for the newbie question - I write several languages but my
JavaScript is very limited. I have a problem with some code which
displays an svg file received via Ajax. The top-level script includes
these lines:
1 var svg = xhr.responseXML.documentElement;
2 svg = cloneToDoc(svg); // portable importNode
3 window.svgRoot = svg;
4 document.body.appendChild(svg);
5 delete window.svgRoot; // <- problem
The script then terminates. The incoming Ajax message ('svg') is a
script, that references a couple of additional scripts, and I rely on
one of them to be run when it's loaded (presumably on 'appendChild',
but I'm not sure).
This works, but only if line 5 is commented out. If it's left in, I
get various issues which have the feel of a memory over-write. I don't
really know what to do here, because I don't understand the window
object, or the scoping issues, or much else. The guy who wrote it says
that line 5 is "just cleanup, removing the global svgRoot property
from the window after appending the file".
Dumb question #1: what exactly is 'window'? Is it an object which
represents the currently visible window? Presumably it's still in
scope, and global, and represents the same window when the other (svg)
scripts are running?
#2: what is line 3 doing? Is it auto-declaring a new 'svgRoot' var in
'window'? Can you just do that? Is the author just finding a
convenient location to put a global?
#3: the rest of the code manipulates the svg via window.svgRoot,
rather than getting a reference to the new child in the document body.
Is this Ok? Is line 4 appending a reference to svg, or copying svg, or
somehting else? Presumably line 5 doesn't actually delete the new
child?
#5: is there a memory overwite/leak detect tool for JavaScript?
Thanks if you managed to read this far...
JavaScript is very limited. I have a problem with some code which
displays an svg file received via Ajax. The top-level script includes
these lines:
1 var svg = xhr.responseXML.documentElement;
2 svg = cloneToDoc(svg); // portable importNode
3 window.svgRoot = svg;
4 document.body.appendChild(svg);
5 delete window.svgRoot; // <- problem
The script then terminates. The incoming Ajax message ('svg') is a
script, that references a couple of additional scripts, and I rely on
one of them to be run when it's loaded (presumably on 'appendChild',
but I'm not sure).
This works, but only if line 5 is commented out. If it's left in, I
get various issues which have the feel of a memory over-write. I don't
really know what to do here, because I don't understand the window
object, or the scoping issues, or much else. The guy who wrote it says
that line 5 is "just cleanup, removing the global svgRoot property
from the window after appending the file".
Dumb question #1: what exactly is 'window'? Is it an object which
represents the currently visible window? Presumably it's still in
scope, and global, and represents the same window when the other (svg)
scripts are running?
#2: what is line 3 doing? Is it auto-declaring a new 'svgRoot' var in
'window'? Can you just do that? Is the author just finding a
convenient location to put a global?
#3: the rest of the code manipulates the svg via window.svgRoot,
rather than getting a reference to the new child in the document body.
Is this Ok? Is line 4 appending a reference to svg, or copying svg, or
somehting else? Presumably line 5 doesn't actually delete the new
child?
#5: is there a memory overwite/leak detect tool for JavaScript?
Thanks if you managed to read this far...