Java Reference
In-Depth Information
This code gets the index of the last “-” character in the id string, adds 1 to it, and passes that value to
the substr() method. If you didn't add 1 to the value returned by lastIndexOf(), the result from
substr() would be “-1”. Now that you have the tab's number, you pass it to the showDescription()
function.
function showDescription(num)
{
var descContainer = document.getElementById(“descContainer”);
The tabs' descriptions are added to the <div/> element with an id of descContainer , so as this code
shows, you fi rst retrieve that element using the getElementById() method.
The descriptions are dynamically created by this function, so now you need to create the DOM nodes to
add to the descCounter element. First, create a <div/> element using the createElement() method
of the document object.
var div = document.createElement(“div”);
Now create a text node containing the description for the tab. In this example, the description is simple
and includes the tab's number.
var text = document.createTextNode(“Description for Tab “ + num);
Finally, add the text node to the newly created <div/> element, and then append that element to
descContainer. Use the Node object's appendChild() method to perform both operations.
div.appendChild(text);
descContainer.appendChild(div);
}
JavaScript's usefulness doesn't end with HTML; there are times when you may want or need to open an
XML fi le and read it with JavaScript. Because of the similarities between (X)HTML and XML (namely
that they are structured documents), it's not surprising that you use the DOM to load and read XML
documents, too.
JavaScript and XML
The W3C developed XML for the purpose of describing data rather than to actually display information
in any particular format, which is the purpose of HTML. There is nothing overly special about XML. It is
just plain text with the addition of some XML tags enclosed in angle brackets. You can use any software
that can handle plain text to create and edit XML.
XML is a data-centric language. It not only contains data, but it describes those data by using semantic
element names. The document's structure also plays a part in the description. Unlike HTML, XML is
not a formatting language; in fact, a properly structured XML document is devoid of any formatting
elements. This concept is often referred to as the separation of content and style , and is part of XML's suc-
cess, as it makes the language simple and easy to use.
For example, you can use XML as a data store like a database. In fact, XML is well suited for large and
complex documents because the data are structured; you design the structure and implement it using
Search WWH ::




Custom Search