JavaScript s Core HTML Methods


JavaScript's Core HTML Methods

We've already seen some of the core HTML methods at work, such as the getElementsByTagName method, which returns an array of elements based with the same tag name . Here's an example we saw in Chapter 4, "Handling the Browser Environment":

(Listing 04-11.html on the web site)
 <HTML>      <HEAD>          <TITLE>              Accessing HTML Elements          </TITLE>          <SCRIPT LANGUAGE="JavaScript">              <!--             function getText()              {                  if(navigator.appName == "Netscape") {                  alert("document.form1.text1.value = " + document.form1.text1.value)                   alert("document.forms[0].text1.value = " + document.forms[0].text1.value)                  alert("document.getElementsByName(\"text1\")[0].value = "                      + document.getElementsByName("text1")[0].value)  alert("document.getElementsByTagName(\"INPUT\")[0].value = "   + document.getElementsByTagName("INPUT")[0].value)  }                  if (navigator.appName == "Microsoft Internet Explorer") {                  alert("document.form1.text1.value = " + document.form1.text1.value)                  alert("document.forms[0].text1.value = "                      + document.forms[0].text1.value)                  alert("document.all.text1.value = "                        + document.all.text1.value)                  alert("document.all[\"text1\"].value = "                        + document.all["text1"].value)                  alert("document.all.tags(\"INPUT\")[0].value = "                        + document.all.tags("INPUT")[0].value)                  alert("document.getElementsByName(\"text1\")[0].value = "                      + document.getElementsByName("text1")[0].value)  alert("document.getElementsByTagName(\"INPUT\")[0].value = "   + document.getElementsByTagName("INPUT")[0].value)  }              }              // -->          </SCRIPT>      </HEAD>      <BODY>          <H1>Accessing HTML Elements</H1>          <FORM NAME="form1">              <INPUT TYPE="TEXT" NAME="text1" ID="text1">              <INPUT TYPE="BUTTON" VALUE="Click Me!" ONCLICK="getText()">          </FORM>      </BODY>  </HTML> 

(As you can see in this code, we also saw the getElementsByName method, but that method is a method of the document object only, and is not a core HTML method that applies to all HTML elements and browser objects.) Working with JavaScript's core HTML methods is straightforwardyou just pass any arguments to the method as you would with any method. Handling events, on the other hand, is a different story.



Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net