Aims:
window
object represents the browser windowdocument
object represents the document being displayedlocation
object represents the URL of the document being displayed
history
object represents the window's recently visited URLslinks[]
- all hyperlinks in the documentimages[]
- all img
elementsanchors[]
- all a
elements with a name
or id
attributeforms[]
- all form
elementselements[]
- controls on a form where the tag is
input
, textarea
, select
or button
document.forms[0].elements[0]
document.forms[0].elements[1]
firstChild
lastChild
childNodes
(returns an array)previousSibling
nextSibling
parentNode
getElementById(...)
document.forms[0].childNodes[1].childNodes[1]
document.getElementById("btns").childNodes[3].childNodes[1]
Examples from previous lecture:
function make_it_smile() { document.getElementById("face").src = 'smile.jpg'; } function make_it_frown() { document.getElementById("face").src = 'frown.jpg'; }
animal1.php
, a self-processing pageNote 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>
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; } }
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.<?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)"; } } ?> ...
animal3.js
validates the user's data and decides on the output