Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!94.232.116.12.MISMATCH!feed.xsnews.nl!border-2.ams.xsnews.nl!takemy.news.telefonica.de!telefonica.de!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="ISO-8859-1" Message-ID: <4775241.ypaU67uLZW@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Sun, 13 Nov 2011 18:25:28 +0100 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 7Bit Subject: Re: Difference between findPos("divThis") and findPos(divThis) Newsgroups: comp.lang.javascript References: Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 62 NNTP-Posting-Date: 13 Nov 2011 18:25:28 CET NNTP-Posting-Host: da38b35d.newsspool4.arcor-online.net X-Trace: DXC=:Y7iM3_QIT>TQL:hoD@>T?4IUKDZm8W4\YJN<;?f@h5gMfb<0HL@K4XoXn?V2PbDSBHVY4 X-Complaints-To: usenet-abuse@arcor.de Xref: x330-a1.tempe.blueboxinc.net comp.lang.javascript:8284 Cal Who wrote: > I find that either of the two approches below work.I'd appreciate getting > a little insight into what is going on and the good and bad aspects of > each approach. Thanks > > function findPos(obj) > { > var w = document.getElementById(obj).offsetWidth; > var h = document.getElementById(obj).offsetHeight; > } > > called with: findPos("divThis") > > Or > > function findPos(obj) > { > var w = obj.offsetWidth; > var h = obj.offsetHeight; > } > called with: findPos(divThis) Your OP was hard to read and not even syntactically valid; I have reformatted it and completed it. Please take more care next time. Both approaches work in Internet Explorer and perhaps elsewhere in *Quirks Mode*. That is so because MSHTML, probably to make DOM scripting easier, elements with IDs are explicitly represented in the DOM by properties of the same name of the object referred to by `window'. That object (`window' object for short) is in the scope chain. So if you use the `divThis' identifier and no other object in the scope chain has a property of that name, it will be resolved to the property of the `window' object. The result is a reference to the DOM element object which represents the element. You should not rely on this proprietary behavior, and you should avoid triggering Quirks Mode. That findPos() function call really is pointless, as the local variables will not be visible outside the function here. So unless you want to support IE/MSHTML 4 and NN 4, for which you would need a wrapper function, KISS: var o = document.getElementById("divThis"); var w = o.offsetWidth; var h = o.offsetHeight; But do not rely on that either; read the discussion about element dimensions three days ago. (In general: search, read, post.) BTW, findPos() does not what its name says. PointedEars -- Anyone who slaps a 'this page is best viewed with Browser X' label on a Web page appears to be yearning for the bad old days, before the Web, when you had very little chance of reading a document written on another computer, another word processor, or another network. -- Tim Berners-Lee