JavaScript and the DOM - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

JavaScript and the DOM

Description:

a href='javascript:log('Flip off out of here')' insensitive patch /a in it /p /body ... a href='http://www.soton.ac.uk' Southampton /a . /p This is a ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 24
Provided by: edshare2
Category:
Tags: dom | javascript | href

less

Transcript and Presenter's Notes

Title: JavaScript and the DOM


1
JavaScript and the DOM
  • Les Carr
  • COMP3001

2
Behavioral Layer
  • Web pages have 3 layers
  • Structural/Content Layer (XHTML)
  • The meat and potatoes
  • Presentational Layer (CSS)
  • How things look garnishing the meat and potatoes
    on a pretty plate
  • Behavioral Layer (JavaScript and DOM)
  • How websites behave the meat can jump off the
    plate if you want it to.

3
Client-side Languages
  • User-agent (web browser) requests a web page
  • JavaScript is executed on PC
  • Can affect the Browser and the page itself
  • Web page (with JavaScript) is sent to PC

4
Client-side
  • What kind of things can you do with JavaScript?
  • Validating Form information,
  • i.e., making sure all the fields are complete
    before submitting data back to the server
  • Modifying a web page based on Mouse Events.
  • Can turn a web page into a user interface with
    interactive buttons and controls

5
Server-side Languages
  • User-agent (web browser) requests a web page
  • User never sees the PHP, only the output
  • Cannot affect the browser or client PC
  • Web page (with PHP Output) sent to PC

6
JavaScript
  • lthtmlgt
  • ltheadgt
  • ltscript languagejavascriptgt
  • function myfun()
  • do something
  • lt/scriptgt
  • lt/headgt
  • ltbodygt
  • ltscript languagejavascriptgt
  • myfun()
  • lt/scriptgt
  • lt/bodygt
  • lt/htmlgt
  • Java Functions definitions are embedded in the
    ltheadgt
  • Function calls are placed in the ltbodygt

7
DOM Scripting
  • Key Topics
  • Event Handling
  • The Browser Object
  • Document Object Model
  • the document structure

8
Event Handling
  • JavaScript code can be initiated by browser
    events
  • HTML 4.0 supports lots of events.
  • onclick, onchange, onmousedown, onmousemove, etc.

9
Browser Events
  • onblur an element loses focus, i.e., click on a
    text box, but then you click on something else
    the text box is blurred
  • onchange contents of an element is changed,
    i.e., changing the selection in a drop down menu
  • onfocus an element is clicked or selected
  • onload when the web page is initially loaded
  • onsubmit when a forms submit button is clicked

10
More Browser Events
  • onkeydown immediately when a key is pressed
    down
  • onkeypress if the key is held down, i.e., not
    immediately released
  • onkeyup immediately when a key is released.
  • Sometimes, you want something to happen when the
    key goes down vs. goes up
  • Sometimes, you want to detect a long key press
    SHIFT, CTRL, or ALT

11
Even More Browser Events
  • onmousedown a mouse button is pressed down
  • onmouseup a mouse button is released
  • onmousemove a mouse is moved
  • onmouseout mouse is moved off an element
    (blur)
  • onmouseover mouse is moved on an element
    (focus, hover)
  • Used for hover effects.

12
Example
  • lthtmlgt
  • ltbodygt
  • lth1gtExample Javascript Event Handlerlt/h1gt
  • ltpgtHere is some text with a
  • ltspan onClick"alert('Do not click here')"gt
    sensitive patchlt/spangt
  • in itlt/pgt
  • lt/bodygt
  • lt/htmlgt

13
Example 2
  • lthtmlgt
  • ltheadgtltscript language"JavaScript"gt
  • function log(s)window.statuss
  • lt/scriptgtlt/headgt
  • ltbodygt
  • lth1gtExample Javascript Event Handlerlt/h1gt
  • ltpgtHere is some text with a
  • ltspan onMouseOver"log('Do not click here')"
    onMouseOut"log(' ')" gt sensitive patchlt/spangt
  • in itlt/pgt
  • lt/bodygt
  • lt/htmlgt

14
Example 3
  • lthtmlgt
  • ltheadgtltscript language"JavaScript"gt
  • function log(s)window.statuss
  • lt/scriptgtlt/headgt
  • ltbodygt
  • lth1gtExample Javascript Event Handlerlt/h1gt
  • ltpgtHere is some text with an
  • lta href"javascriptlog('Flip off out of
    here')"gt insensitive patchlt/agt
  • in itlt/pgt
  • lt/bodygt
  • lt/htmlgt

15
DOM Scripting
  • First a summary
  • JavaScript can be initiated by browser events.
  • JavaScript can access and manipulate the browser
    object.
  • Whats Next
  • JavaScript can access the document structure.

16
DOM Example
  • ltpgt This is a paragraph linking to lta
    href"http//www.soton.ac.uk"gtSouthamptonlt/agt.
  • lt/pgt

Element Nodes
TextNodes
p
AttributeNode
This is a paragraph linking to
a
.
href"http//www.soton.ac.uk/"
Southampton
17
DOM Script Example
  • ltbodygt
  • ltdiv idmenugt
  • lth1gtMain Menult/h1gt
  • lt/divgt
  • ltdiv idcontentgt
  • lth1gtPage Titlelt/h1gt
  • ltpgtBlaa blaa blaa.lt/pgt
  • lt/divgt
  • ...

body
div idcontent
div idmenu
h1
h1
p
Main Menu
Page Title
Blaa blaa blaa.
18
DOM Script Example
  • lthtmlgt
  • ltdiv id"menu"gt
  • lth1gtMain Menult/h1gt
  • lt/divgt
  • ltdiv id"content"gt
  • lth1gtPage Titlelt/h1gt
  • ltpgtBlaa blaa blaa.lt/pgt
  • lt/divgt
  • ltscript language"JavaScript"gt
  • var contentdiv document.getElementById('content'
    )
  • var pagetitle contentdiv.getElementsByTagName('h
    1')0
  • pagetitle.setAttribute("style","colorred")
  • pagetitle.firstChild.nodeValue"The Red Page
    Title"
  • lt/scriptgtlt/htmlgt

19
DOM Script Example
  • var themenu document.getElementById(menu)
  • var thebody menu.parent
  • var thecontent menu.nextSibling
  • var contentnodes thecontent.childNodes
  • var theh1 contentnodes0
  • var firstparagraph contentnodes1

body
div idcontent
div idmenu
h1
h1
p
Main Menu
Page Title
Blaa blaa blaa.
20
DOM Scripting Functions
  • Modifying Structure
  • insertBefore()
  • appendChild()
  • replaceChild()
  • removeChild()
  • cloneNode()
  • Creating Elements
  • createElement()
  • createTextNode()
  • Modifying Attributes
  • getAttribute() / setAttribute()

21
DOM and Forms
  • Every form in a page is held in an array
  • document.forms0 is the first form
  • Every component (input, select or textarea
    element) is held in a subarray
  • document.forms0.elements0 is the first field
  • ltform name"personal"gt
  • ltinput type"text" name"name"gt
  • ltinput type"text" name"address"gt
  • ltinput type"text" name"city"gt
  • lt/formgt

either document.forms0.elements1 or
document.forms"personal".elements"address" or
document.personal.address
22
DOM and Forms
  • Every component of the form has a value
  • document.personal.address.value
  • The value can be used in expressions or stored in
    an assignment statement
  • Specific components have specific methods or
    properties
  • a menu (ie a select) has property selectedIndex
  • a checkbox has property checked
  • An onsubmit event handler can check its forms
    components and halt the submission by returning
    false

23
DOM and Forms
  • ltform name"personal" onSubmit"validate()"gt
  • ltinput type"text" name"name"/gt
  • ltinput type"text" name"address"/gt
  • ltinput type"text" name"city"/gt
  • ltinput type"submit" value"Submit!"/gt
  • lt/formgt
  • ltscript lang"JavaScript"gt
  • function validate()
  • if(document.personal.name.value.length0)
  • alert("Missing name")
  • return false
  • return true
  • lt/scriptgt
Write a Comment
User Comments (0)
About PowerShow.com