0 index
1 Motivation
2 DOM
3 Object Oriented Design
4 Objects have Properties
5 'Core' Object
6 Sample Syntax
7 Note on Properties
8 Methods
9 Example
10 Events
11 Example
12 Important Notes
13 Functions
14 Example
15 Example continued
16 Functions Continued
17 Input/Output - the Event-DrivenWay
18 Getting Input from Other Objects
19 Radio Buttons
20 Arrays
21 Selection Objects
22 Selection Objects Continued
23 Selection Objects Continued
24 Section Objects

outline
created using slideshow.cgi by Andy Harris















CSCI N341 Web Programming: Document Object Model
1. Motivation
  • Connect JavaScript with HTML
  • JavaScript communicates with the page it 'lives in'
  • Achieved through Document Object Model (DOM)



































CSCI N341 Web Programming: Document Object Model
2. DOM
  • A model for describing a web page that already exists
  • DOM heavily dependant on object oriented design
  • The page, all the constituent elements in it and the browser are considered objects



































CSCI N341 Web Programming: Document Object Model
3. Object Oriented Design
  • Object Oriented Programming (OOP)
  • Think of various parts of a program as objects



































CSCI N341 Web Programming: Document Object Model
4. Objects have Properties
  • Consider physical world

      Example: Stapler has properties
      - color
      - staple size
      - number of staples
      - syntax: alert (myStapler.numberOfStaples);



































CSCI N341 Web Programming: Document Object Model
5. 'Core' Object
  • The Document is the core object
  • A web page is essentially a document
  • Documents have properties
  • Example:
      - background color



































CSCI N341 Web Programming: Document Object Model
6. Sample Syntax
  • <center>
    <h1>Background Switcher</h1>
    This page will change colors!! </center>
    <script>
    document.bgColor = "red";
    alert ("Press for a new color");
    document.bgColor = "orange";
    alert ("Press for a new color");
    document.bgColor = "yellow";
    alert ("Press for a new color");
    </script>



































CSCI N341 Web Programming: Document Object Model
7. Note on Properties
  • Not all have the same behavior
  • Some can be read from and written to
  • Others can be read, but not changed by a script (property.domain)
  • Consult up-to-date reference manuals to determine which properties will be available



































CSCI N341 Web Programming: Document Object Model
8. Methods
  • In addition to properties, objects have methods
  • Properties are like adjectives, methods are like verbs
  • Methods are the things an object can do



































CSCI N341 Web Programming: Document Object Model
9. Example
  • <center>
    <h1>Document Methods Example</h1>
    </center>
    <script>
    var userName;
    userName = prompt ("What is your name?");
    document.write("<h3>Hello, " + userName + "! </h3>");
    </script>



































CSCI N341 Web Programming: Document Object Model
10. Events
  • Events are stimuli that an object recognizes
  • Events are basically signals that an object sends out



































CSCI N341 Web Programming: Document Object Model
11. Example
  • <form name = theForm>
    <input type = button
        name = btnHi
        value = "don't click me"
        onClick = 'alert("Och!!")'>
    </form>



































CSCI N341 Web Programming: Document Object Model
12. Important Notes
  • We used a form and give the form and all the form objects names using the name attribute.
  • We put a button in the form, adding onClick parameter
  • Objects that might generate events have an onClick parameter



































CSCI N341 Web Programming: Document Object Model
13. Functions
  • The 'one line' of JavaScript you are allowed is great, however, most of the time you will need to do things that are more involved



































CSCI N341 Web Programming: Document Object Model
14. Example
  • <script>
    function sayOuch(){
       //this 'mini' program says ouch
       //it can be as many lines long as I wish
       alert ("Ouch!!");
    }//end function
    </script>



































CSCI N341 Web Programming: Document Object Model
15. Example continued
  • <form name = theForm>
    <input type = button
        name=btnHi
        value = "Don't click me"
        onClick = "sayOuch()">
    </form>



































CSCI N341 Web Programming: Document Object Model
16. Functions Continued
  • Functions are nothing more than grouping together several lines of code and giving it a name
  • In the same way that a variable is a place-holder for data, a function is a place-holder for instructions
  • Functions are usually stored in the header area so they will be defined before they are needed in code



































CSCI N341 Web Programming: Document Object Model
17. Input/Output - the Event-DrivenWay
  • One method of input (the prompt command)
  • Two methods of output (alert and document.write())
  • document.write() method has some problems
    - the user is not guaranteed to see the output of the command until the page has finished loading



































CSCI N341 Web Programming: Document Object Model
18. Getting Input from Other Objects
  • Text-like objects
    - checkboxes
  • Generally, when you work with checkboxes, you will want to do some kind of a conditional check on the checked property of the item



































CSCI N341 Web Programming: Document Object Model
19. Radio Buttons
  • Radio buttons work in groups - only one item in a radio group can be selected
  • Mechanism is to give all radio objects the same name property
  • Several items with the same name is an example of an array
  • George Foreman scenario- George[1], George[2], etc.



































CSCI N341 Web Programming: Document Object Model
20. Arrays
  • Think of the entire list of radio buttons as one element
  • We are using the loop to look at the elements individually
  • Once this is done, we can extract the value property that was stored in the HTML



































CSCI N341 Web Programming: Document Object Model
21. Selection Objects
  • Important to recognize that the select object is one object and it contains an array of option objects
  • The selected property of the selected tell which element in the object's array was selected, so we do not need a loop



































CSCI N341 Web Programming: Document Object Model
22. Selection Objects Continued
  • Extract the value through a series of steps
    - create a bunch of variables
    - use a variable to hold the selection object
    - another variable to hold the index of the item the user selects
    - another variable for the item itself
    - finally a variable for the output



































CSCI N341 Web Programming: Document Object Model
23. Selection Objects Continued
  • Assign theSelect the value of the selection object
  • The selection object has a selectedIndex property which returns the numeric index of whatever item was selected
  • The select object has a number of option elements attached to it - these elements are essentially a list



































CSCI N341 Web Programming: Document Object Model
24. Section Objects
  • Number lists, in programming, are referred to as arrays denoted by [..] brackets



































outline

Motivation

DOM

Object Oriented Design

Objects have Properties

'Core' Object

Sample Syntax

Note on Properties

Methods

Example

Events

Example

Important Notes

Functions

Example

Example continued

Functions Continued

Input/Output - the Event-DrivenWay

Getting Input from Other Objects

Radio Buttons

Arrays

Selection Objects

Selection Objects Continued

Selection Objects Continued

Section Objects