Datasheet
document.form1.button.value = “Click Me”;
Or, you can use methods that can access the specific elements or subsets of elements on the page, such as
the
document.getElementById method, which will return a specific instance of an element that
matches the criteria:
var myTextBox = document.getElementById(“myTextbox”);
You can then assign values to the variable you have created to alter the values. To make the text box
invisible, you could call the following:
myTextBox.style.visibility = “visible”;
Another related method is the getElementsByTagName method. The getElementsByTagName method
will return an array of elements on the web page of type
NodeList, all with a given tag name, even if
there is only one occurrence of that element on the page. The following code will return all the image
elements on the page:
var imageElements = document.getElementsByTagName(“img”);
It is also possible to assemble the page by adding new sections to the document known as nodes. These
can be elements, attributes, or even plain text. For example, you could create a span tag that contains a
short message and add it to the page as follows:
var newTag = document.createElement(“span”);
var newText = document.createTextNode(“Here is some New Text. Ho Hum.”);
newTag.appendChild(newText);
document.body.appendChild(newTag);
All of these DOM techniques are applicable to the client side, and as a result, the browser can update the
page or sections of it instantly. Ajax leans on these capabilities very heavily to provide the rich user expe-
rience. Also, as mentioned, these techniques have been around since version 4 of IE. It’s just that they
have been underused. The DOM is an important topic, and it is discussed in much more detail in
Chapter 2.
JavaScript
JavaScript is the scripting language of choice of most web developers. You must know JavaScript to be
able to use this book. This book doesn’t teach JavaScript, although a quick refresher of the most salient
points is available in Chapter 2.
Ajax techniques aren’t solely the preserve of JavaScript. VBScript also offers the same capabilities for
dynamic updates as well — albeit tied to IE only. While JavaScript has a standard specified in the
ECMAScript standard, JavaScript was initially created in Netscape Navigator before such standards
existed. Microsoft created its own version of JavaScript (called JScript) in parallel, and as a result, each
browser’s version of JavaScript is slightly different. Although JavaScript remains a very powerful
method for updating your web pages, some amount of dual-coding is necessary to make sure that the
web pages and applications function in the correct way across browsers, When not possible, some error-
handling code will also be necessary.
11
Chapter 1: Introducing Ajax
04_106754 ch01.qxp 2/9/07 6:15 PM Page 11