Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0 Chapter 5 Host Objects: Browsers.

Slides:



Advertisements
Similar presentations
The jQuery library. What is jQuery ? A javascript lightweight library Is very easy to use Is powerful Is cross-browser compatible Downloadable from jQuery.com,
Advertisements

Chapter 6 © 2012 by Addison Wesley Longman, Inc Introduction Def: A dynamic HTML document is one whose tag attributes, tag contents, or element.
The Web Warrior Guide to Web Design Technologies
Tutorial 15 Working with the Event Model. XP Objectives Compare the IE and W3C event models Study how events propagate under both event models Write a.
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 3 Style Sheets: CSS WEB.
20-Jun-15 JavaScript and HTML Simple Event Handling.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
JavaScript, Third Edition
Cos 381 Day 7. © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Agenda Assignment 2 Posted –Program a web-based Version of Soduku using JavaScript.
XP Tutorial 8 New Perspectives on JavaScript, Comprehensive1 Working with the Event Model Creating a Drag-and-Drop Shopping Cart.
Introduction to scripting
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.
 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.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
Browsers and the DOM CSI 3140 WWW Structures, Techniques and Standards.
CITS1231 Web Technologies JavaScript and Document Object Model.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
Lesson13. JavaScript JavaScript is an interpreted language, designed to function within a web browser. It can also be used on the server.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Javascript II DOM & JSON. In an effort to create increasingly interactive experiences on the web, programmers wanted access to the functionality of browsers.
Introduction to JavaScript 41 Introduction to Programming the WWW I CMSC Winter 2004 Lecture 17.
DOM Events. JS Events Outline About Events – Introduction – Registering events – Getting information from events Event order/phases – Preventing default.
Chapter 6 Server-side Programming: Java Servlets
Introduction to the Document Object Model DOM *Understand document structure manipulation *Dynamic effects in web pages *Programming, scripting, web content.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 10.
Cs332a_chapt10.ppt CS332A Advanced HTML Programming DHTML Dynamic Hypertext Markup Language A term describing a series of technologies Not a stand-a-lone.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
Scripting and Interactivity 이병희
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.
Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling – Inline – Scripting –
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 7 TH EDITION Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
Host Objects: Browsers and the DOM
Chapter 6 © 2014 by Pearson Education Introduction Def: A dynamic HTML document is one whose tag attributes, tag contents, or element style properties.
Events Event Handling in JavaScript SoftUni Team Technical Trainers Software University
Browsers and the DOM CSI 3140 WWW Structures, Techniques and Standards.
Dr. Ahmet Cengizhan Dirican BIL 374 Internet Technologies 6. Dynamic Documents With JavaScript.
Chapter 6 Murach's JavaScript and jQuery, C6© 2012, Mike Murach & Associates, Inc.Slide 1.
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 7 Representing Web Data:
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
Chapter 6 Dynamic Documents with JavaScript. © 2006 Pearson Addison-Wesley. All rights reserved Introduction Def: A dynamic HTML document is.
Introduction to JavaScript Events Instructor: Sergey Goldman 1.
Internet & World Wide Web How to Program, 5/e.  JavaScript events  allow scripts to respond to user interactions and modify the page accordingly  Events.
Chapter 6 © 2003 by Addison-Wesley, Inc Introduction - Dynamic HTML is not a new markup language - A dynamic HTML document is one whose tag attributes,
Overview  The Document Object Model (DOM) is an API that allows programs to interact with HTML (or XML) documents In typical browsers, the JavaScript.
REEM ALMOTIRI Information Technology Department Majmaah University.
6.1 Introduction 6.2 Element Positioning
DHTML.
JavaScript and HTML Simple Event Handling 11-May-18.
Introduction to JavaScript Events
Web Development & Design Foundations with HTML5 7th Edition
JavaScript Functions.
Document Object Model (DOM): Objects and Collections
JavaScript and HTML Simple Event Handling 19-Sep-18.
Chapter 7 Representing Web Data: XML
Working with the Event Model
JavaScript and Events CS Programming Languages for Web Applications
JavaScript.
CHAPTER 6 EVENTS. CHAPTER 6 EVENTS WHAT IS AN EVENT?
6.1 Introduction 6.2 Positioning Elements
JavaScript and HTML Simple Event Handling 26-Aug-19.
JavaScript and Events CS Programming Languages for Web Applications
JavaScript and HTML Simple Event Handling 4-Oct-19.
Presentation transcript:

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 5 Host Objects: Browsers and the DOM WEB TECHNOLOGIES A COMPUTER SCIENCE PERSPECTIVE JEFFREY C. JACKSON 1

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Overview The Document Object Model (DOM) is an API that allows programs to interact with HTML (or XML) documents –In typical browsers, the JavaScript version of the API is provided via the document host object –W3C recommendations define standard DOM Several other browser host objects are informal, de facto standards –alert, prompt are examples –Many others are covered in this chapter 2

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved DOM Introduction Example: “Rollover” effect Cursor not over imageImage changes when cursor moves over 3

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved DOM Introduction 4

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved DOM Introduction Import JavaScript code 5

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved DOM Introduction Default language for scripts specified as attribute values 6

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved DOM Introduction Calls to JavaScript show() function when mouse moves over/away from image 7

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved DOM Introduction Notice that id of image is first argument to show() 8

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved DOM Introduction 9

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved DOM Introduction DOM method returning Object 10

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved DOM Introduction Returns instance of Element (DOM-defined host object) representing HTML element with given id 11

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved DOM Introduction Some properties of Element instance correspond to attributes of HTML element 12

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved DOM Introduction Method inherited by Element instances for setting value of an attribute 13

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved DOM Introduction Effect: src attribute of HTML element with specified eltId is changed to specified URL 14

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved DOM Introduction Image src changed to CFP22.png when mouse is over image, CFP2.png when leaves 15

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved DOM History and Levels Very simple DOM was part of Netscape 2.0 Starting with Netscape 4.0 and IE 4.0, browser DOM API’s diverged significantly W3C responded quickly with DOM Level 1 (Oct 1998) and subsequently DOM Level 2 This chapter: JavaScript API for DOM2 + some coverage of browser specifics 16

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Intrinsic Event Handling An event is an occurrence of something potentially interesting to a script: –Ex: mouseover and mouseout events An HTML intrinsic event attribute is used to specify a script to be called when an event occurs –Ex: onmouseover –Name of attribute is on followed by event name 17

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Intrinsic Event Handling 18

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Intrinsic Event Handling 19

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Intrinsic Event Handling 20

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Intrinsic Event Handling Intrinsic event attribute value is a script; what language is it written in? HTTP Content-Script-Type header field specifies default scripting language meta element allows document to specify values as if they were header fields Header field name Header field value 21

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Modifying Element Style Change background color of element containing cursor 22

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Modifying Element Style 23

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Modifying Element Style Like rollover, style needs to be modified both when entering and exiting the element. 24

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Modifying Element Style Reference to Element instance representing the td element 25

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Modifying Element Style 26

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Modifying Element Style Reference to Element instance 27

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Modifying Element Style All Element instances have a style property with an Object value 28

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Modifying Element Style Properties of style object correspond to CSS style properties of the corresponding HTML element. 29

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Modifying Element Style Rules for forming style property names from names of CSS style properties: –If the CSS property name contains no hyphens, then the style object’s property name is the same Ex: color color –Otherwise, all hyphens are removed and the letters that immediately followed hyphens are capitalized Ex: background-color backgroundColor 30

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Modifying Element Style Net effect: “silver” becomes the specified value for CSS background-color property of td element; browser immediately modifies the window. 31

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Modifying Element Style Alternative syntax (not supported in IE6): 32

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Modifying Element Style Alternate syntax (not supported in IE6): Every DOM2-compliant style object has a setProperty() method 33

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Modifying Element Style Alternate syntax (not supported in IE6): CSS property name (unmodified) CSS property value Empty string or “important” 34

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Modifying Element Style Advantages of setProperty() syntax: –Makes it clear that a CSS property is being set rather than merely a property of the style object –Allows CSS property names to be used as-is rather than requiring modification (which can potentially cause confusion) 35

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Modifying Element Style Obtaining specified CSS property value: Alternate DOM2 syntax (not supported by IE6): 36

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Document Tree Recall that HTML document elements form a tree structure, e.g., DOM allows scripts to access and modify the document tree 37

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Document Tree: Node There are many types of nodes in the DOM document tree, representing elements, text, comments, the document type declaration, etc. Every Object in the DOM document tree has properties and methods defined by the Node host object 38

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Document Tree: Node 39

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Document Tree: Node 40

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Document Tree: Node 41

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Document Tree: Node 42

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Document Tree: Node Example HTML document Function we will write that will use Node methods and properties to produce string representing Element tree 43

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Document Tree: Node String produced by TreeOutline() : 44

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Document Tree: Node Example: “walking” the tree of an HTML document –Reference to html element is contained in documentElement property of document object –Use Node -defined methods to recursively create an outline of nodeName ’s: Depth in tree 45

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Document Tree: Node 46

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Document Tree: Node Contains nodeType value representing Element 47

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved DOM Event Handling Note: IE6 has a different event model Event instance created for each event Event instance properties: –type : name of event (click, mouseover, etc.) –target : Node corresponding to document element that generated the event (e.g., button element for click, img for mouseover). This is the event target. 48

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved DOM Event Handling JavaScript event listener: function that is called with Event instance when a certain event occurs An event listener is associated with a target element by calling addEventListener() on the element 49

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved DOM Event Handling 50

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved DOM Event Handling Event target 51

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved DOM Event Handling Event type 52

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved DOM Event Handling DOM event types: –All HTML intrinsic events except keypress, keydown, keyup, and dblclick –Also has some others that are typically targeted at the window object: 53

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved DOM Event Handling Event handler Definition of event handler 54

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved DOM Event Handling Event instance 55

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved DOM Event Handling Normally false (more later) 56

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved DOM Event Handling 57

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved DOM Event Handling: Mouse Events DOM2 mouse events –click –mousedown –mouseup –mousemove –mouseover –mouseout Event instances have additional properties for mouse events 58

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved DOM Event Handling: Mouse Events 59

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved DOM Event Handling: Mouse Events Example: mouse “trail” 60