1 | <HTML>
|
---|
2 | <!-- This file is part of the HTML Reference Library ('HTMLib') -->
|
---|
3 | <!-- It should not be used outside of the HTMLib package -->
|
---|
4 | <!-- The HTMLib is © 1995-1998 Stephen Le Hunte -->
|
---|
5 | <!-- htmlib@htmlib.demon.co.uk -->
|
---|
6 | <HEAD>
|
---|
7 | <LINK REL="stylesheet" HREF="../style.css" TYPE="text/css">
|
---|
8 | <TITLE>The All collection</TITLE>
|
---|
9 | </HEAD>
|
---|
10 | <BODY TOPMARGIN="0" BGCOLOR="#FFFFE0" TEXT="#000000">
|
---|
11 |
|
---|
12 | <CENTER>
|
---|
13 | <SPAN CLASS="NSRtitle">The All Collection</SPAN>
|
---|
14 | </CENTER>
|
---|
15 |
|
---|
16 | <P><STRONG><EM>Note : </EM></STRONG>The All collection is <STRONG>Internet Explorer</STRONG> 4.0 specific. As <STRONG>Netscape</STRONG> doesn't support scripting for <EM>every</EM> HTML element, it doesn't support the All collection.
|
---|
17 |
|
---|
18 | <P>The All collection is an ordered, indexed array, containing a reference to every opening HTML element in a document. For example, consider the following document:
|
---|
19 |
|
---|
20 | <SPAN CLASS="egcode">
|
---|
21 | <BLOCKQUOTE>
|
---|
22 | <CODE><HTML><BR>
|
---|
23 | <HEAD><BR>
|
---|
24 | <TITLE>Test Document</TITLE><BR>
|
---|
25 | </HEAD><BR>
|
---|
26 | <BODY><BR>
|
---|
27 | <P>This is a test<BR>
|
---|
28 | <HR><BR>
|
---|
29 | </BODY><BR>
|
---|
30 | </HTML>
|
---|
31 | </CODE>
|
---|
32 | </BLOCKQUOTE>
|
---|
33 | </SPAN>
|
---|
34 |
|
---|
35 | <P>The all collection would be indexed from 0 to 5, with the elements being positioned:
|
---|
36 |
|
---|
37 | <P>
|
---|
38 | <TABLE WIDTH="60%">
|
---|
39 | <COLGROUP SPAN="2" ALIGN="center">
|
---|
40 | <TR>
|
---|
41 | <TD><STRONG>Element</STRONG></TD>
|
---|
42 | <TD><STRONG>All index</STRONG></TD>
|
---|
43 | </TR>
|
---|
44 | <TR>
|
---|
45 | <TD><CODE><HTML></CODE></TD>
|
---|
46 | <TD>0</TD>
|
---|
47 | </TR>
|
---|
48 | <TR>
|
---|
49 | <TD><CODE><HEAD></CODE></TD>
|
---|
50 | <TD>1</TD>
|
---|
51 | </TR>
|
---|
52 | <TR>
|
---|
53 | <TD><CODE><TITLE></CODE></TD>
|
---|
54 | <TD>2</TD>
|
---|
55 | </TR>
|
---|
56 | <TR>
|
---|
57 | <TD><CODE><BODY></CODE></TD>
|
---|
58 | <TD>3</TD>
|
---|
59 | </TR>
|
---|
60 | <TR>
|
---|
61 | <TD><CODE><P></CODE></TD>
|
---|
62 | <TD>4</TD>
|
---|
63 | </TR>
|
---|
64 | <TR>
|
---|
65 | <TD><CODE><HR></CODE></TD>
|
---|
66 | <TD>5</TD>
|
---|
67 | </TR>
|
---|
68 | </TABLE>
|
---|
69 |
|
---|
70 | <P>Element Object would normally be retrieved by their index in the All collection (for example, above <CODE>document.all(3)</CODE> contains a reference to the <CODE><BODY></CODE> element), but a string value can be used, as long as that string is a valid identifier (<CODE>ID</CODE> attribute value) for an element in the document.
|
---|
71 |
|
---|
72 | <P>E.g., changing the above example to:
|
---|
73 |
|
---|
74 | <SPAN CLASS="egcode">
|
---|
75 | <BLOCKQUOTE>
|
---|
76 | <CODE><HTML><BR>
|
---|
77 | <HEAD><BR>
|
---|
78 | <TITLE ID="Title1">Test Document</TITLE><BR>
|
---|
79 | </HEAD><BR>
|
---|
80 | <BODY><BR>
|
---|
81 | <P>This is a test<BR>
|
---|
82 | <HR><BR>
|
---|
83 | </BODY><BR>
|
---|
84 | </HTML>
|
---|
85 | </CODE>
|
---|
86 | </BLOCKQUOTE>
|
---|
87 | </SPAN>
|
---|
88 |
|
---|
89 | <P>...would mean that <CODE>document.all('Title1')</CODE> would be a reference to the <CODE><TITLE></CODE> element (whose <CODE>ID</CODE> is 'Title1'). This would be the same as <CODE>document.all(2)</CODE>. Note that it is also equivalent to using <CODE>document.all.item('Title1')</CODE> (see the item method below).
|
---|
90 |
|
---|
91 | <P><STRONG><EM>Note : </EM></STRONG>The All collection <STRONG>always</STRONG> includes a reference to <CODE><HTML></CODE>, <CODE><HEAD></CODE> and <CODE><BODY></CODE> elements, whether they exist in the document or not.
|
---|
92 |
|
---|
93 | <P><A NAME="properties"><STRONG>Properties</STRONG></A><BR>
|
---|
94 |
|
---|
95 | <P><SPAN CLASS="attr">length</SPAN><BR>
|
---|
96 | The <CODE>length</CODE> property returns the number of elements in the collection. Note that the <CODE>length</CODE> count starts at 1, not 0 as the all collection index does. Therefore, the <CODE>length</CODE> property may return a value of 5, but to access the 3rd element, you'd need to use <CODE>document.all(2).<EM>property</EM></CODE>
|
---|
97 |
|
---|
98 | <P><A NAME="methods"><STRONG>Methods</STRONG></A>
|
---|
99 |
|
---|
100 | <P><SPAN CLASS="attr">item</SPAN><BR>
|
---|
101 | The <CODE>item</CODE> method retrieves single items, or sub-collections from the all collection. It accepts the following arguments:
|
---|
102 |
|
---|
103 | <BLOCKQUOTE><EM>all</EM>.<STRONG>item</STRONG><EM>(index, sub-index)</EM>
|
---|
104 | </BLOCKQUOTE>
|
---|
105 |
|
---|
106 | <P>If <CODE>index</CODE> is a number, then the method returns a reference to the element object at that position in the all collections index. I.e. (using the example above)
|
---|
107 |
|
---|
108 | <SPAN CLASS="egcode">
|
---|
109 | <BLOCKQUOTE>
|
---|
110 | <CODE>strTag=document.all.item(2).tagName
|
---|
111 | </CODE>
|
---|
112 | </BLOCKQUOTE>
|
---|
113 | </SPAN>
|
---|
114 |
|
---|
115 | <P>would make <CODE>strTag</CODE> be <CODE>TITLE</CODE> - the value of the <CODE><TITLE></CODE> elements <CODE>tagName</CODE> property. As you can see, this is effectively the long-hand version of using <CODE>document.all(2).<EM>property</EM></CODE>.
|
---|
116 |
|
---|
117 | <P>If the <CODE>index</CODE> property is a string value, then the <CODE>item</CODE> method returns a sub-collection, containing a reference to every element in the document that has its <CODE>ID</CODE> or <CODE>NAME</CODE> attribute set to the string contained in the <CODE>index</CODE> argument. To retrieve certain element objects from this sub-collection, the <CODE>sub-index</CODE> argument must be used. For example, assume that 5 elements in a document all have the <CODE>NAME</CODE> attribute set as <CODE>radColour</CODE> (these imaginary elements are radio buttons). To interrogate the 3rd of these elements, you could use:
|
---|
118 |
|
---|
119 | <SPAN CLASS="egcode">
|
---|
120 | <BLOCKQUOTE>
|
---|
121 | <CODE>strThird=document.all('radColour',2).<EM>propertyName</EM>
|
---|
122 | </CODE>
|
---|
123 | </BLOCKQUOTE>
|
---|
124 | </SPAN>
|
---|
125 |
|
---|
126 | <P><A NAME="methtags"><SPAN CLASS="attr">tags</SPAN></A><BR>
|
---|
127 | The <CODE>tags</CODE> method returns a collection of element objects whose <CODE>tagName</CODE> property is the same as the <CODE>tag</CODE> argument used for the method. This differs from the <CODE>item</CODE> property in that that interrogates <CODE>ID</CODE> and <CODE>NAME</CODE> values if necessary.
|
---|
128 |
|
---|
129 | <SPAN CLASS="egcode">
|
---|
130 | <BLOCKQUOTE>
|
---|
131 | <CODE>document.all.tags('EM')
|
---|
132 | </CODE>
|
---|
133 | </BLOCKQUOTE>
|
---|
134 | </SPAN>
|
---|
135 |
|
---|
136 | <P>would return a collection of all the <CODE><EM></CODE> objects in the document.
|
---|
137 |
|
---|
138 |
|
---|
139 | <P>
|
---|
140 | <P ALIGN="right" CLASS="copyright">© 1995-1998, Stephen Le Hunte</P>
|
---|
141 | </BODY>
|
---|
142 | </HTML>
|
---|