The Document Object Model

Aims:

The Document Object Model (DOM)

The Legacy DOM (simplified)

[A diagram showing a simplified version of the Document Object Model]

Legacy DOM objects

Legacy DOM collections

Legacy DOM example

The W3C DOM (simplified)

W3C DOM example

W3C DOM example: changing the document

Examples from previous lecture:

function make_it_smile()
{
	document.getElementById("face").src = 'smile.jpg';
}

function make_it_frown()
{
	document.getElementById("face").src = 'frown.jpg';
}

Version 1: animal1.php, a self-processing page

Note that this PHP script does no validation of user data

<?php 
 $year = '';
 $output = '';
 $animals = array('Monkey', 'Rooster', 'Dog', 'Pig', 
                  'Rat', 'Ox', 'Tiger', 'Rabbit', 
                  'Dragon', 'Snake', 'Horse', 'Goat');
 if ( isset($_GET['submitted']) ) 
 {
    $year = $_GET['year'];
    $index = $year % 12;
    $output = "You are a {$animals[$index]} (roughly speaking)";
 }
?>
<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<!DOCTYPE html 
 PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>When were you born?</title>
  <script type="text/javascript" src="year_validate.js">
  </script>
</head>

<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="get">
 
  <p>
   <label for="yearField">In what year were you born?</label>
   <input type="text" name="year" id="yearField" 
          value="<?php echo $year; ?>"
          size="4" maxlength="4" />
  </p>

  <p>
   <input type="submit" name="submitted" value="Submit" />
  </p>
  
  <p id="output">
   <?php echo $output ?>
  </p>

</form>

</body>
</html>

Version 1: year_validate.js, client-side JavaScript

This JavaScript validates the user's data before submitting it to animal1.php.
Note the use of the W3C DOM

if ( window.addEventListener )
{
   window.addEventListener("load", init, false);
}
else if ( window.attachEvent )
{
   window.attachEvent("onload", init);
}

function init()
{
   document.forms[0].onsubmit = validate;
}

function validate()
{
   var year;
   year = document.getElementById("yearField").value;
   if ( ! /^\d{4}/.test(year) ||  year > (new Date()).getFullYear() )
   {
      document.getElementById("output").firstChild.nodeValue = 'ERROR: You must enter a valid year';
      return false;
   }
}

Version 2: animal2.php, belt and braces!

animal2.php uses year_validate.js to perform validation of user data but also validates the user's data all over again on the server.
Why might this be better?
<?php 
 $year = '';
 $output = '';
 $animals = array('Monkey', 'Rooster', 'Dog', 'Pig', 
                  'Rat', 'Ox', 'Tiger', 'Rabbit', 
                  'Dragon', 'Snake', 'Horse', 'Goat');
 if ( isset($_GET['submitted']) ) 
 {
    $year = $_GET['year'];
    if ( trim($year) == '' || ! is_numeric($year) || $year < 0 || $year > date('Y') )
    {
       $output = "ERROR: You must enter a valid year.";
    }
    else
    {
       $index = $year % 12;
       $output = "You are a {$animals[$index]} (roughly speaking)";
    }
 }
?>
...

Version 3