JavaScript (Part 8)- Dynamic Web Pages Using JavaScript- DOM and Events
JavaScript (Part 8)- Dynamic Web Pages Using JavaScript- DOM and Events
Ullrich Hustadt
2 Event-driven Programs
Introduction
Events
3 Extended Example
4 Further Reading
Arnaud Le Hors, et al, editors: Document Object Model (DOM) Level 3 Core Specification, Version 1.0,
W3C Recommendation 07 April 2004. World Wide Web Consortium, 2004.
https://www.w3.org/TR/DOM-Level-3-Core/ [accessed 9 January 2017]
COMP519 Web Programming Lecture 17 Slide L17 – 3
Dynamic Web Pages Using JavaScript Document Object and Document Object Model
// access its second tr element ; the list of children starts at 0 ( not 1).
var mySecondTrEle ment = myTbodyElement . childNodes [1];
Then – document.form1
Refers to the form named form1
– document.form1.celsius
Refers to the text field named celsius in document.form1
– document.form1.celsius.value
Refers to the attribute value in the text field named celsius
in document.form1
COMP519 Web Programming Lecture 17 Slide L17 – 5
Dynamic Web Pages Using JavaScript Accessing and Manipulating HTML Elements
Then
– document.getElementById('celsius')
Refers to the HTML element with ID celsius document
– document.getElementById('celsius').value
Refers to the attribute value in the HTML element with ID celsius
in document
• With JavaScript,
– we can define event handler functions for a wide variety of events
– event handler functions can manipulate the document object
(changing the web page in situ)
COMP519 Web Programming Lecture 17 Slide L17 – 11
Event-driven Programs Introduction
More than one event handler can be added this way to the same
element for the same event or different events
COMP519 Web Programming Lecture 17 Slide L17 – 12
Event-driven Programs Introduction
Events: Load
• An (on)load event occurs when an object has been loaded
• Typically, event handlers for onload events are associated with the
window object or the body element of an HTML document
<! DOCTYPE html >
< html lang =" en - GB " >
< head >
< title > Onload Example </ title >
< script >
function Hello () { alert (" Welcome to my page !") }
</ script >
</ head >
< body onload =" Hello ()" >
<p > Content of the web page </ p >
</ body >
</ html >
http://cgi.csc.liv.ac.uk/~ullrich/COMP519/examples/jsOnload.html
http://cgi.csc.liv.ac.uk/~ullrich/COMP284/examples/jsOnclick.html
• The interface will consist of a 3x3 table representing the board and a
section for messages, both of which will be generated dynamically
< body >
< table id =" t1 " > </ table >
< div id =" m1 " > </ div >
< script >... </ script >
</ body >
function clearMessage () {
m1 = document . getElementById (" m1 ");
m1 . style . display = " none ";
}
• Arguments x and y are the co-ordinates on which the player as placed a piece
• event is the event that was triggered and event.target gives us the
HTML element / table cell on which it was triggered
COMP519 Web Programming Lecture 17 Slide L17 – 23
Extended Example
function sleep ( ms ) {
return new Promise ( resolve = > setTimeout ( resolve , ms )) }
COMP519 Web Programming Lecture 17 Slide L17 – 26
Extended Example