Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.42!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder2-2.proxad.net!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <8927386.SEqChMirdb@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Sat, 22 Oct 2011 14:35:56 +0200 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 8Bit Subject: Re: Get value of named object Newsgroups: comp.lang.javascript Followup-To: comp.lang.javascript References: Supersedes: <1549126.qVoOGUtdWV@PointedEars.de> MIME-Version: 1.0 Lines: 73 NNTP-Posting-Date: 22 Oct 2011 14:35:57 CEST NNTP-Posting-Host: 9731af31.newsspool1.arcor-online.net X-Trace: DXC=SWlfYZV_TV@gj[ZPFj7ehOic==]BZ:afN4Fo<]lROoRA<`=YMgDjhgB<4l0U4HNHNFDZm8W4\YJNLb@mF9jNikdE_dXMBNGELEAieZkROFc4HK X-Complaints-To: usenet-abuse@arcor.de Xref: x330-a1.tempe.blueboxinc.net comp.lang.javascript:7617 Swifty wrote: > If I have the name of an object in a variable, That is _not_ the name of an object (objects have _identity_, not name). It is a possible reference path of a property (a property accessor) which value refers to that object. > how would I go about getting the value of that object? > > Example: > varname = 'navigator.appName'; > > How would I get the value of navigator.appName ? eval(varname). But you really don't want to do that. > See http://swiftys.org.uk/jsvars.html for my first, faltering attempt. > > The idea is to replace the multiple "document.write" lines with the > simpler "show()" calls. (Your current show() would still cause document.write() to be called consecutively, which is a bad idea.) Consider this: var properties = [ ["navigator", "appCodeName"], ["screen", "availHeight", "availWidth"], ["window", "closed", "history", "innerHeight", "innerWidth", ["location", "hostname", "pathname"], "outerHeight", "outerWidth"] ]; Then iterate recursively over this data structure, starting with _global[properties[i][0]] where `_global' is a reference to the ECMAScript global object (`this' in the global execution context) and `i' is the iterator variable. Complete and improve this: var out = []; for (var i = 0, len = properties.length; i < len; ++i) { var properties2 = properties[i]; var base = properties2[0]; for (var j = 1, len2 = properties2.length; j < len2; ++j) { var property = properties2[j]; out.push( "…" + base + "." + property + "…" + _global[base][property] + "…"); } } document.write("…" + out.join("…") + "…"); You may use Object instances as well if you do not care about order or if you iterate over property values in an order that you define. In any case, you have to take care of inherited and special properties, then. Since you are dealing with host objects here, you have to be extra careful with "unknown"-type properties and to catch any exceptions a property access can throw. PointedEars -- Use any version of Microsoft Frontpage to create your site. (This won't prevent people from viewing your source, but no one will want to steal it.) -- from (404-comp.)