~~SLIDESHOW~~ ====== JavaScript and HTML Documents: Part 1 ====== **Contact Hour 11**: To be discussed on Wednesday 20th February, 2013. **Lecturer**: [[C.P.Jobling@Swansea.ac.uk|Dr Chris P. Jobling]]. This lecture describes some of the client-side web programming features of JavaScript. ===== JavaScript and HTML Documents: Part 1 ===== * Client-side JavaScript defines a collection of objects, methods and properties that allow scripts to interact with HTML documents on the client. * This lecture describes some of these features. ---- The slides and notes in this lecture are based on Chapter 5 of Robert W. Sebasta, //Programming the World-Wide Web//, 3rd Edition, Addison Wesley, 2006. and Chapter 7 of Chris Bates, //Web Programming: Building Internet Applications//, 3rd Edition, John Wiley, 2006. ===== Contents of this Lecture ===== Manipulating web documents through the Document Object Model (DOM) and the JavaScript event model. * [[eg-259:lecture8#javascript_execution_environment|JavaScript Execution Environment]] * [[eg-259:lecture8#the_document_object_model|The Document Object Model]] * [[eg-259:lecture8#element_access_in_javascript|Element Access in JavaScript]] * [[eg-259:lecture8#events_and_event_handling|Events and Event Handling]] * [[eg-259:lecture8#handling_events_from_body_elements|Handling Events from Body Elements]] * [[eg-259:lecture8#handling_events_from_button_elements|Handling Events from Button Elements]] ===== Learning Outcomes ==== //At the end of this lecture you should be able to answer these questions//: - Global variables in JavaScript are properties of what object? - How are HTML elements and attributes represented in the JavaScript binding to DOM? - What is an event? - What is an event handler? ===== Learning Outcomes (continued) ===== //At the end of this lecture you should be able to answer these questions//: - What are the two ways in which an event handler can he associated with an event generated by a specific HTML element in the DOM 0 event model? - Why should ''document.write'' not be used in an event handler? - In what ways can an HTML element acquire focus? ===== Learning Outcomes (continued) ===== //At the end of this lecture you should be able to answer these questions//: - Describe the approach to addressing HTML elements using forms and elements. - Describe the approach to addressing HTML elements using ''name'' attributes. - Describe the approach to addressing HTML elements using ''getElementById''. ===== JavaScript Execution Environment ===== * The JavaScript ''Window'' object represents the window in which the browser displays documents * The ''Window'' object provides the largest enclosing referencing environment for scripts * All //global// variables are properties of ''Window'' * Implicitly defined ''Window'' properties: * ''document'' -- a reference to the ''Document'' object that the window displays * ''frames'' -- an array of references to the frames of the document ===== The Document object ===== * Every Document object has: * ''forms'' -- an array of references to the forms of the document * Each ''Form'' object has an ''elements'' array, which has references to the form's elements * ''Document'' also has arrays for ''anchors'', ''links'', and ''images'' ===== The Document Object Model ===== * DOM 0 is supported by all JavaScript-enabled browsers (no written specification) * DOM 1 was released in 1998 * DOM 2 is the latest approved standard -- supported by most (all?) modern browsers * The DOM is an abstract model that defines the interface between HTML documents and application programs -- an API * Latest [[http://www.w3.org/DOM/|DOM specifications ]]are kept at the W3C ---- DOM 2 is the latest approved standard: * Nearly completely supported by Mozilla-based browsers * IE6 support is lacking some important things * Will be complete in IE7? The HTML5 specifications are intended to be much more precise about what the DOM is and how it should react to user behaviours. ===== A simple document ===== A simple document
Breakfast 0 1
Lunch 1 0
Load code [[http://localhost:4567/eg-259/examples/lecture8/dom.html|dom.html]] ===== DOM Structure for Simple Document ===== {{eg-259:l8-dom.png|The DOM for the example document}} ===== The Document Object Model (continued) ===== * A language that supports the DOM must have a binding to the DOM constructs * In the //JavaScript binding//, HTML elements are represented as //objects// and element attributes are represented as //properties// ===== DOM Element as JavaScript object ===== For example: would be represented as an ''input'' object with two properties, ''type'' and ''name'', with the values ''%%"text"%%'' and ''%%"address"%%''. E.g: input = { type: "text", name: "address" }; ===== Element Access in JavaScript ===== There are several ways to do it * [[#element_access_by_array_index|Element access by array index]] * [[#element_access_by_name|Element access by name]] * [[#element_access_by_id|Element access by id]] ===== An Example ===== Example (a document with just one form and one widget):
Rendered as: Load code [[http://localhost:4567/eg-259/examples/lecture8/form.html|form.html]] ===== 1. Element Access by Array Index ===== * DOM address: var button = document.forms[0].elements[0] * **Problem**: solution is fragile if document changes, indices may be wrong ---- For example if another form is added //before// this one, or another widget is added //before// the button. ===== 2. Element Access by Name ===== * requires the element and all of its ancestors (except body) to have ''//name//'' attributes
* Button //pushMe// can be accessed by: var button = document.forms[0].pushMe ---- * **Problem**: XHTML 1.1 specification doesn't allow the ''name'' attribute on form elements! * HTML5 does allow name and a form can be addressed by name if either //name// or //id// is set (presumably //name// is used if both are present). ===== 3. Element Access by Id ===== * ''getElementById'' method (defined in DOM 1)
* Button now accessed as: var button = document.getElementById("pushMe") ---- This is the safest method, and is the most widely adopted convention. It does require that each widget be given a unique id. ===== 4. Element Access by CSS selector - jQuery ===== * Using jQuery CSS selector (more later)
* Button now accessed as: var button = $("#pushMe"); ---- As jQuery has become almost a standard part of the developer's toolbox, this way of accessing a DOM element by CSS selector is almost certain to work. You do need to include jQuery as an external library of course. ===== Check Box/Radio Button Access ===== * Check boxes and radio buttons have an implicit array, which has their name
Olives
Tomatoes
* Rendered: Olives
Tomatoes Load code [[http://localhost:4567/eg-259/examples/lecture8/radiobuttons.html|radiobuttons.html]] ===== Check Box/Radio Button Access (continued) ===== * Check-box items could be accessed by this script: var numChecked = 0; var dom = document.getElementById("topGroup"); for (index = 0; index < dom.toppings.length; index++) { if (dom.toppings[index].checked) { numChecked++; } } ---- **Mini exercise**: Modify the above to generate an array of toppings. ===== Events and Event Handling ===== * An event is a notification that something specific has occurred, either with the browser or an action of the browser user * An event handler is a script that is implicitly executed in response to the appearance of an event * The process of connecting an event handler to an event is called //registration// * Don't use ''document.write'' in an event handler, because the output will probably replace the current DOM ===== The Standard Events and their Handlers ===== ^ Event ^ Tag Attribute ^ Event ^ Tag Attribute ^ | ''blur'' | ''onblur'' | ''mouseout'' | ''onmouseout'' | | ''change'' | ''onchange'' | ''mouseover'' | ''onmouseover'' | | ''click'' | ''onclick'' | ''mouseup'' | ''onmouseup'' | | ''focus'' | ''onfocus'' | ''select'' | ''onselect'' | | ''load'' | ''onload'' | ''submit'' | ''onsumbit'' | | ''mousedown'' | ''onmousedown'' | ''unload'' | ''onunload'' | | ''mousemove'' | ''onmousemove'' | | | ===== Events and Event Handling (continued) ===== * The same attribute can appear in several different tags * e.g., The ''onclick'' attribute can be in '''' and '''' ===== Handling Events from Text Elements ===== A text element gets focus in three ways: * When the user puts the mouse cursor over it and presses the left button * When the user //tabs// to the element * By executing the //focus// method ===== Event Attributes: onblur ===== * The ''//onblur//'' attribute ^ Tag ^ Description ^ | '''' | The link loses focus | | ''