Solved

determine all defined image object attributes

Posted on 2008-10-11
4
846 Views
Last Modified: 2008-10-11
Is there a way to enumerate all attributes associated with an image, or for that matter the attributes associated with any other element in a web page?  I know I can access attributes if I specify them, eg. the source of an image as  document.images[0].src, or the width as document.images[0].width, but what if I want to list every attribute?  Have attached a little code below where I loop through every image in a page, and identify each of four attributes.  But rather than explicitly naming the attributes I want (which in some cases finds attributes undefined, like useMap for image 2), is there a way to write an inner loop, from attribute = 0 to (number of defined attributes), similar to the way the outer loop is from 0 to (number of images)?  That way the code could generically be applied to any object, without naming the specific attributes.

<html>

<head>

<title>Enumerate Image Attributes</title>

</head>
 

<body >

<img src="image1.jpg" usemap="#Map1" name="Image1" width="400" height="240" border="0" id="Image1" />

<img src="image2.jpg" name="Image2" width="200" height="120" border="0" id="Image2" />
 

<SCRIPT LANGUAGE="JavaScript">

for(i=0;i<document.images.length;i++) 

{

alert("image "+ i + " source is "+document.images[i].src);

alert("image "+ i + " id is "+document.images[i].id);

alert("image "+ i + " width is "+document.images[i].width);

alert("image "+ i + " map is "+document.images[i].useMap);

}

</SCRIPT>

</body>

</html>

Open in new window

0
Comment
Question by:yessirnosir
  • 2
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
bluV11t earned 500 total points
Comment Utility
Something like this would do the trick, I think.
<SCRIPT LANGUAGE="JavaScript">

for(i=0;i<document.images.length;i++) 

{

    for(j=0;j<document.images[i].attributes.length;j++)

    {alert("image "+ i + " element "+document.images[i].attributes[j].name + ', value: ' +document.images[i].attributes[j].value);}

}

</SCRIPT>

Open in new window

0
 
LVL 14

Author Comment

by:yessirnosir
Comment Utility
Thanks bluV11t, that's perfect.  Quick follow-up if you don't mind -- can you direct me to any reference info that would have helped me figure that out for myself?  Before I posted I studied this Document Object Model reference page (http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-642250288) and googled things like "document images attributes" but didn't manage to find that critical "attributes.length" property that you have just shown me.
0
 
LVL 7

Expert Comment

by:bluV11t
Comment Utility
I used to have a dom inspector plugin for IE that was a lot of help some versions back but it stopped working in IE 6. Tried a few javascript debuggers but havent really found one that suits me yet. Usually I just code what I think will work and then test it. If it doesn't work I fiddle with it till it works ;-)

In regards to "attributes.length" I guess any collection (images, elements...) has this property but I could of course be mistaken.
0
 
LVL 14

Author Comment

by:yessirnosir
Comment Utility
Thanks again bluV11t... now that you've set me on the right track I was more successful googling and did find some documentation, eg. http://developer.mozilla.org/en/DOM/element.attributes
Then I also discovered that the attributes "collection" is handled very differently in IE vs other browsers.  In IE6 all the undefined values are included in the attributes array as nulls, so while my test code under Firefox, Safari, or Opera only generates only 7 alerts for the first image, under IE6 it generates 99!  
Anyway, thanks for helping me along my learning curve.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now