Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news-peer.in.tum.de!news.belwue.de!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="ISO-8859-1" Message-ID: <3004336.SPkdTlGXAF@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Thu, 03 Nov 2011 20:11:15 +0100 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 7Bit Subject: Re: Newbie Q's: references, scopes, overwrites Newsgroups: comp.lang.javascript References: Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 121 NNTP-Posting-Date: 03 Nov 2011 20:11:16 CET NNTP-Posting-Host: 13d171f2.newsspool4.arcor-online.net X-Trace: DXC=Rd8I1IF@;f=n`gW2MTm]<34IUKDmU61YU:iFm_XJ; X-Complaints-To: usenet-abuse@arcor.de Xref: x330-a1.tempe.blueboxinc.net comp.lang.javascript:7958 Dom Bannon wrote: > 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. That is to be expected. But does it terminate with an error? If yes, what does the error message say? > 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 is not going to happen. > This works, It is generally unreliable. The only way to be rather sure that new script code will be executed is to append a new `script' element. This might be an exception if the parent document is declared XHTML 1.1 + SVG + MathML, or HTML5, and you are actually appending an `svg' element node, where the script that it contains pertains to and is triggered by SVG elements only. But your description indicates otherwise. > but only if line 5 is commented out. Line 5 attempts to delete a property of a host object, which may not be possible. > If it's left in, I get various issues which have the feel of a memory > over-write. What issues? How did you get that impression? > I don't really know what to do here, because I don't understand the window > object, or the scoping issues, or much else. This does not appear to have anything to do with scope. > The guy who wrote it says that line 5 is "just cleanup, removing the > global svgRoot property from the window after appending the file". That description is correct in general, but they are otherwise clueless. See below. > Dumb question #1: what exactly is 'window'? It is a host-defined property of the ECMAScript Global Object which value is a reference to an object that represents the view of the document that contains the script code. > Is it an object which represents the currently visible window? In a sense, yes. > Presumably it's still in scope, and global, and represents the same window > when the other (svg) scripts are running? This is not a valid question. Please restate your request. > #2: what is line 3 doing? It is an attempt to create or overwrite the property of the object referred to by `window', that is named `svgRoot', with the value of the `svg' variable. > Is it auto-declaring a new 'svgRoot' var in 'window'? No. > Can you just do that? No. > Is the author just finding a convenient location to put a global? Yes, and he might have been misguided by Douglas Crockford's notion that this was the (proper) way to do that. > #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? That depends on whether the object referred to by `window' already had a `svgRoot' property in the first place, whether it has one after the assignment in line 3, and whether the value of that property is the value of `svg'. The property creation may fail because the object referred to by `window' is a host object (not a native one), and the property value modification may fail because an existing property of that name was defined to be read-only. > Is line 4 appending a reference to svg, or copying svg, or > somehting else? Yes. > Presumably line 5 doesn't actually delete the new child? Please restate your request. > #5: is there a memory overwite/leak detect tool for JavaScript? Yes. PointedEars -- Danny Goodman's books are out of date and teach practices that are positively harmful for cross-browser scripting. -- Richard Cornford, cljs, (2004)