Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / JScript / August 2004

Tip: Looking for answers? Try searching our database.

Iterating through object collections

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mark Rae - 07 Aug 2004 09:40 GMT
Hi,

Is there any way in JavaScript to iterate through an object's properties to
interrogate their values? I'm thinking specifically of the navigator object,
the screen object, the window object and the document object. Something
like:

foreach(obj in screen.properties)
{
   document.write(obj.name + " = " + obj.value)
}

Any assistance gratefully received.

Mark
Martin Honnen - 07 Aug 2004 13:53 GMT
> Is there any way in JavaScript to iterate through an object's properties to
> interrogate their values? I'm thinking specifically of the navigator object,
[quoted text clipped - 5 lines]
>     document.write(obj.name + " = " + obj.value)
> }

  for (var property in window.screen) {
    document.write('<p>' + property + ': ' + window.screen[property] +
'<\/p>');
  }

Signature

    Martin Honnen
    http://JavaScript.FAQTs.com/

Mark Rae - 07 Aug 2004 15:18 GMT
>    for (var property in window.screen) {
>      document.write('<p>' + property + ': ' + window.screen[property] +
> '<\/p>');
>    }

Excellent! Thanks.
Mark Rae - 07 Aug 2004 16:12 GMT
>    for (var property in window.screen) {
>      document.write('<p>' + property + ': ' + window.screen[property] +
'<\/p>');
>    }

Actually, when I came to try this, I couldn't get it to work. I replaced
"for" with "foreach", but that didn't help... :-(
Martin Honnen - 07 Aug 2004 16:55 GMT
>>   for (var property in window.screen) {
>>     document.write('<p>' + property + ': ' + window.screen[property] +
[quoted text clipped - 4 lines]
>
> Actually, when I came to try this, I couldn't get it to work.

The code is correct, if you try that with Netscape for instance it shows
you properties that you expect.
The problem with
  for (var property in object)
is that it enumerates all properties of the object that are _marked as
being enumerable_ and there is no standard for screen specifying whether
its properties should be marked as being enumerable. Thus if you run the
code with IE/Win for instance and nothing shows up then it is because
the IE implementors have decided to mark the properties of screen as
being not enumerable.

There is nothing you can do about that with your code, but of course
IE's object model is well documented so look into the SDK documentation
first for the properties it defines for an object.

Signature

    Martin Honnen
    http://JavaScript.FAQTs.com/

Mark Rae - 07 Aug 2004 18:13 GMT
> There is nothing you can do about that with your code, but of course
> IE's object model is well documented so look into the SDK documentation
> first for the properties it defines for an object.

OK - thanks.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.