Internet & World Wide Web How to Program, 5/e. ©1992-2012 by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.

Slides:



Advertisements
Similar presentations
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
Advertisements

JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
Document Object Model (DOM) JavaScript manipulation of the DOM.
HTML 5 and CSS 3, Illustrated Complete Unit L: Programming Web Pages with JavaScript.
Lesson 12- Unit L Programming Web Pages with JavaScript.
Tutorial 16 Working with Dynamic Content and Styles.
Chapter 16 Dynamic HTML and Animation The Web Warrior Guide to Web Design Technologies.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Cos 381 Day 9.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
Tutorial 11 Working with Operators and Expressions
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Cos 381 Day 10.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Events.
DHTML - Introduction Introduction to DHTML, the DOM, JS review.
DHTML. What is DHTML?  DHTML is the combination of several built-in browser features in fourth generation browsers that enable a web page to be more.
HTML DOM.  The HTML DOM defines a standard way for accessing and manipulating HTML documents.  The DOM presents an HTML document as a tree- structure.
JS: Document Object Model (DOM)
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 Dynamic Documents With JavaScript.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 JavaScript and HTML Documents.
DOM and JavaScript Aryo Pinandito.
Lecture 11 – DOM Scripting SFDV3011 – Advanced Web Development Reference: 1.
Creating an Animated Web Page
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 34 - Case Study: Active Server Pages and XML Outline 34.1 Introduction 34.2 Setup and Message.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
JavaScript – The DOM JavaScript is object based The browser is object based – We can access the browser's objects in the same way we did JavaScript's Two.
JavaScript, Fourth Edition
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
5.2 DOM (Document Object Model). 2 Motto: To write it, it took three months; to conceive it three minutes; to collect the data in it — all my life. —F.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.
JavaScript, Fourth Edition Chapter 4 Manipulating the Browser Object Model.
1 Javascript DOM Peter Atkinson. 2 Objectives Understand the nature and structure of the DOM Add and remove content from the page Access and change element.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
Advanced DOM Builds on last presentation on DOM Allows you to dynamically create elements and position them on a page DOM methods/properties are W3C standard.
Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling – Inline – Scripting –
Dr. Ahmet Cengizhan Dirican BIL 374 Internet Technologies 5. JavaScript and HTML Documents.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
Advanced DOM Builds on last presentation on DOM Allows you to dynamically create elements and position them on a page DOM methods/properties are W3C standard.
Host Objects: Browsers and the DOM
Tutorial 11 1 JavaScript Operators and Expressions.
Dr. Ahmet Cengizhan Dirican BIL 374 Internet Technologies 6. Dynamic Documents With JavaScript.
Create Element, Remove Child. The Document Tree Document Element Root Element Element Element Element Element Text: HelloWorld Attribute “href”
Chapter 10 Dynamic HTML (DHTML) JavaScript, Third Edition.
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Making web pages interactive with JavaScript.
XML DOM Week 11 Web site:
Internet & World Wide Web How to Program, 5/e.  JavaScript events  allow scripts to respond to user interactions and modify the page accordingly  Events.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
Introduction to JavaScript DOM Instructor: Sergey Goldman.
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 5 Host Objects: Browsers.
XP Tutorial 10 New Perspectives on JavaScript, Comprehensive 1 Working with Dynamic Content and Styles Creating a Dynamic Table of Contents.
REEM ALMOTIRI Information Technology Department Majmaah University.
THE DOM.
Introduction to JavaScript DOM and Events
DHTML.
Programming Web Pages with JavaScript
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
JavaScript Functions.
Document Object Model (DOM): Objects and Collections
Revision.
DHTML Javascript Internet Technology.
JavaScript and HTML Documents
DHTML Javascript Internet Technology.
Document Object Model (DOM): Objects and Collections
Introduction to DHTML, the DOM, JS review
Chengyu Sun California State University, Los Angeles
Web Programming and Design
Presentation transcript:

Internet & World Wide Web How to Program, 5/e

© by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.3 Revised by Dr. T. Tran for CSI3140

 The Document Object Model (DOM) is an API that allows programs to interact with HTML (or XML) documents  JavaScript programs access the DOM through a host object named document  So, the DOM gives you scripting access to all the elements on a web page. Using JavaScript, you can create, modify and remove elements in the page dynamically. © by Pearson Education, Inc. All Rights Reserved. 4 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.5 Revised by Dr. T. Tran for CSI3140

 getElementById( String ) method  Returns an object called DOM node corresponding to the HTML element with the id attribute specified by the String argument, or returns null if no document element has the specified id attribute  Every piece of an HTML5 page (elements, attributes, text, etc.) is modeled/represented by a DOM node  The DOM nodes of a document make up the document’s DOM tree, which describes the relationships among elements  Nodes are related to each other through child-parent relationships  A node can have multiple children, but only one parent  Nodes with the same parent node are referred to as siblings  The html node in a DOM tree is called the root node, because it has no parent  Today’s desktop browsers provide developer tools that can display a virtual representation of a document’s DOM tree © by Pearson Education, Inc. All Rights Reserved. 6 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.7 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.8 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.9 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.10 Revised by Dr. T. Tran for CSI3140

 The next example demonstrates several DOM node features and two additional document- object methods.  It allows you to highlight, modify, insert and remove elements.  CSS class “ highlighted” is applied dynamically to elements in the document as we add, remove and select elements using the form. © by Pearson Education, Inc. All Rights Reserved. 11 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.12 Revised by Dr. T. Tran for CSI3140

 We’ll manipulate the HTML5 document dynamically by modifying its DOM. © by Pearson Education, Inc. All Rights Reserved. 13 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.14 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.15 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.16 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.17 Revised by Dr. T. Tran for CSI3140

 The JavaScript code declares two variables  Variable currentNode keeps track of the currently highlighted node—the functionality of each button depends on which node is currently selected.  Variable idcount is used to assign a unique id to any new elements that are created.  The remainder of the JavaScript code contains event-handling functions for the buttons and two helper functions that are called by the event handlers. © by Pearson Education, Inc. All Rights Reserved. 18 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.19 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.20 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.21 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.22 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.23 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.24 Revised by Dr. T. Tran for CSI3140

Finding and Highlighting an Element Using getElementById, setAttribute and getAttribute  The first row of the form allows the user to enter the id of an element into the text field and click the Get By Id button to find and highlight the element. © by Pearson Education, Inc. All Rights Reserved. 25 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.26 Revised by Dr. T. Tran for CSI3140

 The DOM element methods setAttribute and getAttribute allow you to modify an attribute value and get an attribute value, respectively. © by Pearson Education, Inc. All Rights Reserved. 27 Revised by Dr. T. Tran for CSI3140

 document object’s createElement( String ) method  Creates a new DOM node representing an element, given a String argument representing the element type (such as “p”, “div”, etc.). Although this method creates an element, it does not insert the element on the page.  document object’s createTextNode( String ) method  Creates a DOM node that contains only text. Given a String argument, this method inserts the string into the text node.  Method appendChild( Node )  Inserts the argument Node to the end of the list of children of this node (i.e., the node on which this method is called).  Property parentNode contains the parent of this node  insertBefore( Node, Node ) method  Inserts the first argument Node in the list of children of this node, immediately before the second argument Node (or at the end of child list if the second argument is null).  replaceChild( Node, Node ) method  In the list of children of this node, replaces the second argument Node with the first argument Node.  removeChild( Node ) method  Removes the argument Node from this node’s list of children. © by Pearson Education, Inc. All Rights Reserved. 28 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.29 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.30 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.31 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.32 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.33 Revised by Dr. T. Tran for CSI3140

 DOM has collections—groups of related objects on a page  DOM collections are accessed as properties of DOM objects, such as the document object or a DOM node  The document object has properties containing the images collection, links collection, forms collection, and anchors collection  Contain all the elements of the corresponding type on the page  The collection’s length property specifies the number of items in the collection © by Pearson Education, Inc. All Rights Reserved. 34 Revised by Dr. T. Tran for CSI3140

 You access the elements of the collection using indices in square brackets  item method of a DOM collection  An alternative to the square bracketed indices  Receives an integer argument and returns the corresponding item in the collection  namedItem method  receives an element id as an argument and returns the element with that id in the collection.  href property of a DOM link node  Refers to the link’s href attribute  Collections allow easy access to all elements of a single type in a page  Useful for gathering elements into one place and for applying changes across an entire page © by Pearson Education, Inc. All Rights Reserved. 35 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.36 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.37 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.38 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.39 Revised by Dr. T. Tran for CSI3140

 An element’s style can be changed dynamically  E.g., in response to user events  Can create mouse-hover effects, interactive menus and animations  The document object’s body property refers to the body element  The setAttribute method is used to set the style attribute with the user-specified color for the background-color CSS property.  If you have predefined CSS style classes defined for your document, you can also use the setAttribute method to set the class attribute. © by Pearson Education, Inc. All Rights Reserved. 40 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.41 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.42 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.43 Revised by Dr. T. Tran for CSI3140

 The next example introduces the window object’s setInterval and clearInterval methods, combining them with dynamic styles to create animated effects.  This example is a basic image viewer that allows you to select a book cover and view it in a larger size. When the user clicks a thumbnail image, the larger version grows from the top-left corner of the main image area. © by Pearson Education, Inc. All Rights Reserved. 44 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.45 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.46 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.47 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.48 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.49 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.50 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.51 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.52 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.53 Revised by Dr. T. Tran for CSI3140

© by Pearson Education, Inc. All Rights Reserved.54 Revised by Dr. T. Tran for CSI3140

 setInterval method of the window object  Repeatedly executes a statement on a certain interval  Takes two parameters:  A statement to execute repeatedly  An integer specifying how often to execute it, in milliseconds  Returns a unique identifier to keep track of that particular interval.  window object’s clearInterval method  Stops the repetitive calls of object’s setInterval method  Pass to clearInterval the interval identifier that setInterval returned  Anonymous function  Defined with no name—it’s created in nearly the same way as any other function, but with no identifier after the keyword function. © by Pearson Education, Inc. All Rights Reserved. 55 Revised by Dr. T. Tran for CSI3140