1 The Document Object Model. 2 Objectives You will be able to: Describe the structure of the W3C Document Object Model, or DOM. Use JavaScript to access.

Slides:



Advertisements
Similar presentations
1 Web Site Design Overview of the Internet Cookie Setton.
Advertisements

Presenter: James Huang Date: Sept. 26,  Introduction  Basics  Lists  Links  Forms  CSS 2.
Web Design with Cascading Style Sheet Lan Vu. Overview Introduction to CSS Designing CSS Using Visual Studio to create CSS Using template for web design.
11 Getting Started with ASP.NET Beginning ASP.NET 4.0 in C# 2010 Chapters 5 and 6.
Web-based Application Development Lecture 9 February 7, 2006 Anita Raja.
1 The Information School of the University of Washington Nov 1fit dom © 2006 University of Washington Document Object Model (DOM) INFO/CSE 100, Fall.
Chapter 16 Dynamic HTML and Animation The Web Warrior Guide to Web Design Technologies.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
Chapter 3. Table have many uses in a HTML design but are mostly used for the organization of your web site. Tables also give vertical and horizontal structure.
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.
Copyright © Texas Education Agency, All rights reserved. 1 Web Technologies Website Development with Dreamweaver.
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.
Web Technologies Website Development Trade & Industrial Education
1 The Document Object Model SAMS Teach Yourself JavaScript in 24 Hours (Fourth Edition) Michael Moncur SAMS Publishing 2007.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
A really fairly simple guide to: mobile browser-based application development (part 4, JQuery & DOM) Chris Greenhalgh G54UBI / Chris Greenhalgh.
The Document Object Model. Goals Understand how a JavaScript can communicate with the web page in which it “lives.” Understand how to use dot notation.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
UFCEWT-20-3 Advanced Topics in Web Development Lecture 4 : JavaScript, Browsers & the HTML DOM-API.
Adding User Interactivity – Lesson 51 Adding User Interactivity Lesson 5.
Website Development with Dreamweaver
CITS1231 Web Technologies JavaScript and Document Object Model.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Tutorial 7 Creating Forms. Objectives Session 7.1 – Create an HTML form – Insert fields for text – Add labels for form elements – Create radio buttons.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
CA Professional Web Site Development Class 2: Anatomy of a Web Site and Web Page & Intro to HTML.
CSCE 102 – Chapter 6 (Web Design and Layout) CSCE General Applications Programming Benito Mendoza Benito Mendoza 1 By Benito Mendoza.
Document Objects Forms, Images and Links. The document object model Each element of an HTML document, such as an image, form, link, or button, can be.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
1 JavaScript Functions and Objects. 2 Objectives You will be able to Use JavaScript functions in JavaScript scripts. Get JavaScript functions executed.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
1 The Information School of the University of Washington Nov 1fit dom © 2006 University of Washington Document Object Model (DOM)
Chapter 27 Getting “Web-ified” (Web Applications) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Positioning Objects with CSS and Tables
Document Object Model (DOM). Outline  Introduction of DOM  Overview of DOM  DOM Relationships  Standard DOM.
11 Getting Started with ASP.NET Beginning ASP.NET in C# and VB Chapters 1 and 2.
JavaScript Object Model. Biggest Advantage of JavaScript  I can access values of HTML elements using JavaScript  I can modify values of HTML elements.
Chapter 10 Dynamic HTML (DHTML) JavaScript, Third Edition.
IN THIS LESSON, WE WILL BECOME FAMILIAR WITH HTML AND BEGIN CREATING A WEB PAGE IN YOUR COMPUTER HTML – the foundation.
XML DOM Week 11 Web site:
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
COM621: Advanced Interactive Web Development Lecture 6 – JavaScript (cont.)
Getting Started with Dreamweaver
Introduction to.
DHTML.
Javascript and Dynamic Web Pages: Client Side Processing
Applied Component I Unit II Introduction of java-script
Using DHTML to Enhance Web Pages
Document Object Model (DOM)
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Web Development & Design Foundations with HTML5 7th Edition
Programming the Web using XHTML and JavaScript
Document Object Model (DOM)
Getting Started with Dreamweaver
Document Object Model (DOM): Objects and Collections
Revision.
Week 11 Web site: XML DOM Week 11 Web site:
Tutorial 6 Creating Dynamic Pages
Introduction to Programming the WWW I
Working with Dynamic Content and Styles
Document Object Model (DOM): Objects and Collections
Getting Started with Dreamweaver
Floating and Positioning
Introduction to HTML.
CNIT 133 Interactive Web Pags – JavaScript and AJAX
Introduction to JavaScript
Web Programming and Design
JavaScript: BOM and DOM CS Programming Languages for Web Applications
Presentation transcript:

1 The Document Object Model

2 Objectives You will be able to: Describe the structure of the W3C Document Object Model, or DOM. Use JavaScript to access DOM objects. Dynamically modify page content with JavaScript using the DOM API.

3 The DOM All modern browsers hold everything that you see on a page in an elaborate data structure. The Document Object Model You can access the contents from a JavaScript script. Add new content. Modify and delete content sent by the server.

4 The DOM The DOM consists of a hierarchy of JavaScript objects corresponding to the HTML tags that were sent to the browser by the server. Tree structure. "window" object is the root. Everything else is contained in an object included, at some level, in the window. Parent-child relationship. Reflects the containment relationships in the HTML. We can access any object from its parent.

5 The DOM API The JavaScript objects that make up the DOM have public methods and properties. Define an API that scripts can use to interact with the user and modify page content.

6 The window Object When a script is running on a browser, there is always a current window object. The window contains a document object. The document object contains everything we see on the page. window.document refers to the document object of the current window. A global name, always available.

7 The window Object We can refer to the methods and properties of the current window directly without saying "window." Examples: alert('Hello!'); means window.alert('Hello!'); document.write('xxx'); means window.document.write('xxx');

8 Some Properties of window.document document.URL document.title document.referrer document.lastModified document.bgColor document.fgColor

9 Example: Accessing the DOM In Visual Studio, create a new Empty Web Site. DOM_Demo

10 Add HTML Page

11 Add test.html

12 Accessing a document Property

13 Accessing a document Property

14 Accessing Other document Properties Let's add some more content. This document was last modified on: document.write(document.lastModified + " "); document.write("Its title is " + document.title + " "); document.write("And its URL is " + document.URL + " ");

15 Accessing Other document Properties

16 document.write document.write only works correctly as the page is being loaded The script is executed as the browser loads the page. The function argument appears at that point in the rendered page.

17 Examine the DOM Add an HTML page to the website. simple.htm l Delete the and the keeping the page as simple as possible. Add a heading and a paragraph as shown on the next slide. Set this as the starting page.

18 A Simple HTML Page

19 DOM Representation Each box in the diagram represents a DOM node. The boxes above the bottom row correspond to DOM elements. The boxes in the bottom row are text nodes (not elements).

20 Terminology: Elements vs Nodes An element in the DOM corresponds to a start-tag end-tag pair and everything contained within them. Elements can contain other, smaller, elements in a parent-child relationship. Every element is a node in the DOM tree structure, but there are other kinds of nodes as well.

21 Nodes Nodes are the most basic objects in the DOM. The DOM consists of a tree of nodes. Kinds of nodes: Elements Defined by begin-end tag pairs Example:... Attributes Defined inside begin tags Example: Text Nodes Just text Example: This is a heading

22 Elements vs. Nodes Only elements can have children. Attribute nodes and text nodes are always children of element nodes. Cannot have child nodes of their own. No other node can be included in an attribute node or a text node.

23 Nodes JavaScript code can navigate the DOM by following links to child nodes, parent nodes, and sibling nodes. Script can add, modify, and delete nodes. Easier way: assign unique IDs to nodes that we want to act on. Script can then access the node by its ID.

24 Enable Script Debugging We will use the Visual Studio JavaScript Debugger to examine the DOM. Requires IE as browser Enable script debugging in your browser. Tools > Internet Options Advanced tab Browsing Uncheck “Disable Script Debugging (Internet Explorer)”

25 Enable Script Debugging

26 hello.js Add a JavaScript file, hello.js Permit us to use the Visual Studio debugger to examine the DOM. function say_hello() { alert('Hello'); doc = window.document; html = doc.firstChild; }

27 Add the Script to the HTML Page A Simple HTML Document <script language="Javascript" type="text/javascript" src="hello.js"> This is a heading. This is a paragraph.

28 Set a Breakpoint Try it!

29 Breakpoint Hit Right click on html and select Quickwatch

30 Explore the Document End of Section

31 Manipulating Page Content with JavaScript Now let's look at how we can manipulate the page content with JavaScript using the DOM API. Let the user move an object around on the screen by clicking buttons. Hide an object and make it reappear Modify text Change text to an image.

32 Positioning Content with JavaScript

33 Positioning with JavaScript Create a new empty web site. DOM_Positioning Download and add to website: Downloads/205_DOM/ Downloads/205_DOM/ Water_Lilies.jpg animate.js positioning.html Set positioning.html as start page.

34 Website DOM Positioning

35 positioning.html Positioning Elements with JavaScript <script language="javascript" type="text/javascript" src="animate.js"> #square { position:absolute; top: 200px; left: 100px; width: 200px; height: 200px; border: 2px solid black; padding: 10px; background-color: #E0E0E0; } Style for element with specified ID

positioning.html Positioning Elements <input type="button" name="left" value="<- Left" onclick="pos(-1,0);" /> " onclick="pos(1,0);" /> <input type="button" name="up" value="Up" onclick="pos(0,-1);" /> <input type="button" name="down" value="Down" onclick="pos(0,1);" /> <input type="button" name="hide" value="Hide" onclick="hideSquare();" /> <input type="button" name="show" value="Show" onclick="showSquare();" /> This square is an absolutely positioned layer that you can move using the buttons above.

animate.js var x=100,y=200; function pos(dx, dy) { var sq; if (!document.getElementById) return; x += 10*dx; y += 10*dy; sq = document.getElementById("square"); sq.style.top = y; sq.style.left = x; } function hideSquare() { var sq; if (!document.getElementById) return; sq = document.getElementById("square"); sq.style.display = "none"; } function showSquare() { var sq; if (!document.getElementById) return; sq = document.getElementById("square"); sq.style.display="block"; }

38 Try it!