Outline. Lecture 4: Document Object Model (DOM) What is DOM Traversal and Modification Events and Event Handling

Similar documents
JavaScript and XHTML. Prof. D. Krupesha, PESIT, Bangalore

LECTURE-3. Exceptions JS Events. CS3101: Programming Languages: Javascript Ramana Isukapalli

Photo from DOM

Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world

Events: another simple example

DOM Primer Part 2. Contents

COMP519 Web Programming Lecture 16: JavaScript (Part 7) Handouts

JAVASCRIPT BASICS. Handling Events In JavaScript. In programing, event-driven programming could be a programming

CITS3403 Agile Web Development Semester 1, 2018

JavaScript Handling Events Page 1

HTML User Interface Controls. Interactive HTML user interfaces. Document Object Model (DOM)

Fundamentals of Website Development

New Perspectives on Creating Web Pages with HTML. Tutorial Objectives

Introduction to DHTML

LECTURE-2. Functions review HTML Forms. Arrays Exceptions Events. CS3101: Scripting Languages: Javascript Ramana Isukapalli

Programming the Web VALIDATING FORM INPUT

Key features. Nothing to do with java It is the Client-side scripting language Designed to add interactivity to HTML pages

CSC Javascript

TEXTAREA NN 2 IE 3 DOM 1

CSI 3140 WWW Structures, Techniques and Standards. Browsers and the DOM

Overview. Event Handling. Introduction. Reviewing the load Event. Event mousemove and the event Object. Rollovers with mouseover and mouseout

Princeton University COS 333: Advanced Programming Techniques A Subset of JavaScript

Princeton University COS 333: Advanced Programming Techniques A Subset of JavaScript

UNIT - III. Every element in a document tree refers to a Node object. Some nodes of the tree are

Interactor Tree. Edith Law & Mike Terry

Client vs Server Scripting

Continues the Technical Activities Originated in the WAP Forum

CISH-6510 Web Application Design and Development. Overview of JavaScript. Overview

Want to add cool effects like rollovers and pop-up windows?

Govt. of Karnataka, Department of Technical Education Diploma in Computer Science & Engineering. Fifth Semester. Subject: Web Programming

JavaScript is described in detail in many books on the subject, and there is excellent tutorial material at

JSF - H:INPUTSECRET. Class name of a validator that s created and attached to a component

JavaScript II CSCI 311 SPRING 2017

Session 16. JavaScript Part 1. Reading

Installation and Configuration Manual

Web Programming and Design. MPT Junior Cycle Tutor: Tamara Demonstrators: Aaron, Marion, Hugh

CSC Web Programming. JavaScript Browser Objects

JAVASCRIPT FOR PROGRAMMERS

710 Index Attributes, 127 action attribute, 263 assigning, bottom attribute, domain name attribute, 481 expiration date attribute, 480 8

Fall UI Design and Implementation 1

link document.getelementbyid("coffee").style.borderwidth = "0px" document.getelementbyid("tea").style.borderwidth = "10px"

Sections and Articles

XHTML Mobile Profile. Candidate Version Feb Open Mobile Alliance OMA-TS-XHTMLMP-V1_ C

JavaScript and Events

Web Development & Design Foundations with HTML5, 8 th Edition Instructor Materials Chapter 14 Test Bank

CISC 1600 Lecture 2.4 Introduction to JavaScript

Full file at Tutorial 2: Working with Operators and Expressions

Web Design. Lecture 6. Instructor : Cristina Mîndruță Site : Cristina Mindruta - Web Design

CE212 Web Application Programming Part 2

729G26 Interaction Programming. Lecture 4

JavaScript: Events, DOM and Attaching Handlers

Session 6. JavaScript Part 1. Reading

Web Design and Application Development

Programing for Digital Media EE1707. Lecture 4 JavaScript By: A. Mousavi & P. Broomhead SERG, School of Engineering Design, Brunel University, UK

8/19/2018. Web Development & Design Foundations with HTML5. Learning Objectives (1 of 2) Learning Objectives (2 of 2) What is JavaScript?

Chapter 14 - Dynamic HTML: Event Model

Recall: Document Object Model (DOM)

Use Web Event Bubbling

Best Practices Chapter 5

Index. Ray Nicholus 2016 R. Nicholus, Beyond jquery, DOI /

JSF - H:SELECTONEMENU

JSF - H:SELECTONERADIO

Session 7. JavaScript Part 2. W3C DOM Reading and Reference

JSF - H:PANELGRID. JSF Tag. Rendered Output. Tag Attributes. The h:panel tag renders an HTML "table" element. Attribute & Description.

Lecture 3: The Basics of JavaScript. Background. Needs for Programming Capability. Origin of JavaScript. Using Client-side JavaScript

JS Tutorial 3: InnerHTML Note: this part is in last week s tutorial as well, but will be included in this week s lab

Programming the World Wide Web by Robert W. Sebesta

Skyway Builder Web Control Guide

Mouse-over Effects. Text based buttons

Client-side Techniques. Client-side Techniques HTML/XHTML. Static web pages, Interactive web page: without depending on interaction with a server

Introduction to using HTML to design webpages

Interactive XML Visualization - using XSLT 2.0 on the Browser. Phil Fearon - Saxonica

Hell is other browsers - Sartre. The touch events. Peter-Paul Koch (ppk) WebExpo, 24 September 2010

Web Engineering (Lecture 06) JavaScript part 2

Digitizing Sound and Images III Storing Bits

CSS The web browser uses its own resources, and eases the burden on the server. It has fewer features than server side scripting.

INLEDANDE WEBBPROGRAMMERING MED JAVASCRIPT INTRODUCTION TO WEB PROGRAMING USING JAVASCRIPT

Chapter 11 Objectives

Canvas & Brush Reference. Source: stock.xchng, Maarten Uilenbroek

Beijing , China. Keywords: Web system, XSS vulnerability, Filtering mechanisms, Vulnerability scanning.

GRITS AJAX & GWT. Trey Roby. GRITS 5/14/09 Roby - 1

Produced by. App Development & Modeling. BSc in Applied Computing. Eamonn de Leastar

Javascript. A key was pressed OR released. A key was released. A mouse button was pressed.

Walking the DOM Tree. Lecture Outline. An example XHTML page. CSE 190 M (Web Programming), Spring 2008 University of Washington

INTRODUCTION TO WEB DEVELOPMENT AND HTML. Lecture 15: JavaScript loops, Objects, Events - Spring 2011

An updated events syntax for XML-based markup languages

EVENT-DRIVEN PROGRAMMING

Introduction to JavaScript p. 1 JavaScript Myths p. 2 Versions of JavaScript p. 2 Client-Side JavaScript p. 3 JavaScript in Other Contexts p.

Creating Web Pages with HTML-Level III Tutorials HTML 6.01

of numbers, converting into strings, of objects creating, sorting, scrolling images using, sorting, elements of object

Web Development & Design Foundations with HTML5

1$ 5 ! $ 6 4 * Source: 0 "!*! 0! * 8 97 ?!$ 5 0 *! 4! $ 0 : * ' () 7;7 7<7

Hell is other browsers - Sartre. The touch events. Peter-Paul Koch (ppk) DIBI, 28 April 2010

EECS1012. Net-centric Introduction to Computing. Lecture JavaScript Events

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

5. Strict mode use strict ; 6. Statement without semicolon, with semicolon 7. Keywords 8. Variables var keyword and global scope variable 9.

Events. Mendel Rosenblum. CS142 Lecture Notes - Events

Session 17. JavaScript Part 2. W3C DOM Reading and Reference. Background and introduction.

Web Site Development with HTML/JavaScrip

This course is designed for web developers that want to learn HTML5, CSS3, JavaScript and jquery.

Transcription:

Outline Lecture 4: Document Object Model (DOM) What is DOM Traversal and Modification Events and Event Handling Wendy Liu CSC309F Fall 2007 1 2 Document Object Model (DOM) An defined application programming interface (API) between XHTML documents and application programs An abstract model In fact, it is a collection of interfaces Including one for each document tree node type Platform-neutral and language-neutral Support a variety of application programming languages (JavaScript, C++, Java, VB) Can be used in programming to Create documents, navigate the structure, and change/add/delete elements and their content Documents in the DOM have a treelike structure There can be more than one tree in a document (but is unusual) W3C Standard (DOM 1, DOM 2) Implementation is browser dependent Language Binding A support language must have a binding to the DOM constructs Correspondence between constructs in the language and elements in the DOM JavaScript binding Elements of a document are objects Attributes are represented by properties 3 4 1

Example: Element Binding function Element() { // properties and methods of the Node object... // properties and methods of Element object var tagname ; getattribute = elementgetattribute(name); setattribute = elementsetattribute(name, value); removeattribute = elementremoveattribute(name); getatrributenode = elementgetattributenode(name); setatrributenode = elementsetattributenode(newattr);... } Traversal and Modification Window, Document, and Element 5 6 DOM Representation of XML <A> <B>text</B> <C> <D>child of C</D> <E>another child of C</E> </C> <F>moreText</F> </A> Document text Element moretext Text Window The Window object represents the window that displays the document Properties of the Window object are visible to all JavaScript scripts that appear in the window s XHTML document The Window object include all of the global variables as properties It is the unique global object which is created before control enters any execution context Has a self referential property: window All JavaScript variables are properties of some object 7 8 2

Document Represent the displayed XHTML document A central interface Create new elements and text nodes Element Elements of a document are Element objects Can be used to Navigate document Change document tree structure 9 10 Identifying Existing Elements <body> <form name= myform > <p><input type= button name= turniton id= onbutton /></p> <form> </body> Location in tree document.forms[0].element[0] ID document.getelementbyid( onbutton ) Tag name document.getelementsbytagname( input ) Returns a node list (could be empty) Attribute name document.getelementbyname( turniton ) Discouraged in XHTML document.myform.turniton HTML usage; deprecated in XHTML Caution: Inconsistency in the Book Sebesta P89 first paragraph (XHTML 1.1) form elements must still use the name attribute because it is used in processing form data. Sebesta P194 middle two paragraphs the XHTML 1.1 standard does not allow the name attribute in the form element, even though the attribute is now legal for form elements. Although name attributes are not allowed on form elements, name attributes are often required on the elements in a form. The official version ( W3C DOM 2 HTML Specification) getelementsbyname With [HTML 4.01] documents, this method returns the (possibly empty) collection of elements whose name value is given by elementname. In [XHTML 1.0] documents, this method only returns the (possibly empty) collection of form controls with matching name. 11 12 3

Example: DOM Tree Modifying DOM <html xmlns="http://www.w3.org/ 1999/xhtml"> <head><title>navigating DOM</title> <script type"text/javascript">... </script> </head> <body><h1>a Nice Table</h1> <table id="mtable border="1"> <tr><td>1</td><td>2</td></tr> </table><form><input type="button"onclick="showdom()" value="print DOM" /> </form></body></html> 13 Node appendchild(newchild) Add node newchild to the end of child list replacechild(newchild, oldchild) Replace node oldchild with newchild insertbefore(newchild, oldchild) Insert node newchild in child list before node oldchild clonenode(deep) Return a copy of the node including all descendents if deep==true removechild(achild) Remove node achild from child list including all descendents (all methods in Node are available to Document and Element) Document createelement(tagname) Create an Element object of type tagname createtextnode(data) Create a Text object with data Element removeattribute(name) setattribute(name, value) http://www.w3.org/tr/2000/rec-dom-level-2-core-20001113/ecma-script-binding.html 14 Example: Adding Table Rows Events and Event Handling 15 16 4

Event-driven Programming Most, if not all, GUI systems and toolkits are designed to be event driven, meaning that the main flow of your program is not sequential from beginning to end. Robin Dunn Speech at GUI programming at OSCON2004 Structured vs. Event Driven Structured Program Event Driven Architecture Hollywood Principle: "Don't call us; we'll call you."... You implement the interfaces, you get registered. You get called when the time is right. Dafydd Rees http://c2.com/cgi/wiki?hollywoodprinciple 17 18 Event-Driven Execution JavaScript programs are typically event-driven Execution can be triggered by events that occur on the Web page, usually as a result of user actions onclick, ondblclick, onkeydown, onload, onmouseover, onsubmit, onresize, Events are associated with XHTML tag attributes The onclick event can be associated with <a> and form <input> tags Partial List of Events Clipboard oncopy, oncut, onpaste Keyboard onkeydown, onkeyup, onkeypress Mouse onmousedown, onmouseup, onmousemove Other onfocus, onblur, load, unload 19 20 5

Registering Event Handlers By assigning the event handler script to an event tag attribute <a id= mylink href= onmouseover= popup(); > </a> By assigning the event handler script to an event property of an object document.getelementbyid( mylink ).onmouseover = popup; DOM 2 Event Model Modularized interface HTMLEvents abort, blur, change, error, focus, load MouseEvents click, mousedown, mousemove, mouseover Support event propagation Not available in DOM 0 (i.e. HTML event model) 21 22 Event Propagation Basic flow Capturing phase The process by which an event can be handled by one of the event's target's ancestors before being handled by the event's target Capture operates from the top of the tree, generally the Document, dispatching the event downward to the target node At target When the event reaches the target, any event listeners registered on the EventTarget are triggered Bubbling phase The process by which an event propagates upward through its ancestors after being handled by the event's target Bubbling events will then trigger any additional event listeners found by following the EventTarget's parent chain upward, until Document Some events are non-bubbling: load, unload Cancel further propagation Event.stopPropagation(); Can be used in the capture and bubbling phase 23 Example: Event Propagation 24 6

Changing Style Attributes Example: Move Image on Screen CSS1 and CSS-P (CSS - Positioning) are scriptable from JavaScript Allow dynamic style update on XHTML elements The style property position zindex top The effect of the 3 rd left The largest value is on the top visibility backgroundcolor color dimension 25 26 Tracking Mouse Movements Slow Movement of Element Window methods settimeout settimeout( move(), 20); Delays for 20 ms before move() is called setinterval setinterval( move(), 20); Cause move() to be repeatedly executed at 20 ms intervals Track mouse position on screen Drag and drop ball on click Events onmousemove and onclick 27 28 7