HTML and CSS Reference
In-Depth Information
2. Save your changes to the file, and then reload usconst.htm in your Web browser.
As shown in Figure 14-21, the TOC on the Web page should now display a list of
all the heading elements from the source document.
Figure 14-21
list of headings from the source document
text of eac h list item
taken from the h1
through h6 headings
in the sourc e document
Trouble? If you are running a version of Internet Explorer before IE9, you will
receive an error because earlier versions of Internet Explorer do not support the
indexOf() function. To avoid this error message, you should either upgrade your
browser to the current browser version or create your own indexOf() function
using the techniques described in the next InSight box.
Creating an indexOf() Function for Older Browsers
The indexOf() function is supported by all current browsers, but it is not supported by
versions of Internet Explorer before IE9. If your app needs to support Internet Explorer
version 8 and earlier, you can create your own version of the indexOf() function using
the following code:
function indexNum(arr, val) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] == val) return i;
}
return -1;
}
The indexNum() function loops through each item in the supplied array. If a match is
found, it breaks off the for loop and immediately returns the array index number. If
it completes the loop without finding a match, it returns -1. Thus, you also could have
searched through the headings array in the table of contents app using the command
indexNum(headings, n.nodeName);
and achieved the same results as those provided by the indexOf() function. However,
because the indexOf() function is built into JavaScript, it usually will perform the
search quicker and more efficiently.
Search WWH ::




Custom Search