JavaScript Document Object Model 1. Document Object Model (DOM) JavaScript access to the elements of an HTML document. An object hierarchy Provides access.

Slides:



Advertisements
Similar presentations
CT-376 jQuery Most popular javascript library today Latest version:
Advertisements

The Document Object Model (DOM) 1 JavaScript is an object-based language—that is, it’s based on manipulating objects by changing each object’s properties.
1 Lesson 10 Using JavaScript with Styles HTML and JavaScript BASICS, 4 th Edition Barksdale / Turner.
Tutorial 16 Working with Dynamic Content and Styles.
Chapter 16 Dynamic HTML and Animation The Web Warrior Guide to Web Design Technologies.
1 JavaScript Document Object Model & Events. 2 Document Object Model (DOM) JavaScript access to the elements of an HTML document. An object hierarchy.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
Tutorial 13 Working with Objects and Styles. XP Objectives Learn about objects and the document object model Reference documents objects by ID, name,
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
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.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
JavaScript & DOM Client-side scripting. JavaScript JavaScript is a tool to automate client side (which is implemented using HTML so far) JavaSript syntax.
UFCEWT-20-3 Advanced Topics in Web Development Lecture 4 : JavaScript, Browsers & the HTML DOM-API.
Lecture 11 – DOM Scripting SFDV3011 – Advanced Web Development Reference: 1.
Intro to Dreamweaver Web Design Section 7-1 Part or all of this lesson was adapted from the University of Washington’s “Web Design & Development I” Course.
JQUERY | INTRODUCTION. jQuery  Open source JavaScript library  Simplifies the interactions between  HTML document, or the Document Object Model (DOM),
Lab 3: Language Structures User Interface Lab: GUI Lab Sep. 9 th, 2014.
Creating an Animated Web Page
Working with Objects Creating a Dynamic Web Page.
CITS1231 Web Technologies JavaScript and Document Object Model.
XP Tutorial 4 New Perspectives on JavaScript, Comprehensive1 Working with Objects Creating an Animated Web Page.
Manipulating the DOM CST 200 – JavaScript 3 –
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.
Introduction to JavaScript 41 Introduction to Programming the WWW I CMSC Winter 2004 Lecture 17.
JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.
DHTML: Working with Objects Creating a Dynamic Web Page.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
CSS1 Dynamic HTML Objects, Collections & Events. CSS2 Introduction Dynamic HTML treats HTML elements as objects. Element’s parameters can be treated as.
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.
Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling – Inline – Scripting –
1 The Information School of the University of Washington Nov 1fit dom © 2006 University of Washington Document Object Model (DOM)
JavaScript Challenges Answers for challenges
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
EIW: Javascript DOM and Events1 JavaScript Document Object Model & Events.
Lesson 16. Practical Application 1 We can take advantage of JavaScript and the DOM, to set up a form so that the first text box of a form automatically.
School of Computing and Information Systems CS 371 Web Application Programming JavaScript - DOM Modifying the Page from within.
Document Object Model (DOM). Outline  Introduction of DOM  Overview of DOM  DOM Relationships  Standard DOM.
Document Object Model Nasrullah. DOM When a page is loaded,browser creates a Document Object Model of the Page.
JavaScript Object Model. Biggest Advantage of JavaScript  I can access values of HTML elements using JavaScript  I can modify values of HTML elements.
Chapter 6 Murach's JavaScript and jQuery, C6© 2012, Mike Murach & Associates, Inc.Slide 1.
Chapter 10 Dynamic HTML (DHTML) JavaScript, Third Edition.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Making web pages interactive with JavaScript.
1 Objects In JavaScript. 2 Types of Object in JavaScript Built-in objects User Defined Objects Browser Object Document Object Model.
CSE 154 LECTURE 9: THE DOM TREE. The DOM tree The elements of a page are nested into a tree-like structure of objects the DOM has properties and methods.
XML DOM Week 11 Web site:
XP Tutorial 10 New Perspectives on JavaScript, Comprehensive 1 Working with Dynamic Content and Styles Creating a Dynamic Table of Contents.
COM621: Advanced Interactive Web Development Lecture 6 – JavaScript (cont.)
>> JavaScript: Document Object Model
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Week 4: Introduction to Javascript
Scripting the DOM MIS 3502, Fall 2016 Jeremy Shafer Department of MIS
CGS 3066: Web Programming and Design Spring 2017
Document Object Model (DOM): Objects and Collections
CT Web Development, Colorado State University
DHTML Javascript Internet Technology.
DHTML Javascript Internet Technology.
JavaScript.
Web Programming and Design
Web Programming and Design
Presentation transcript:

JavaScript Document Object Model 1

Document Object Model (DOM) JavaScript access to the elements of an HTML document. An object hierarchy Provides access to elements via: – ID ( ID attribute of HTML tags) – Order of elements in document – Order of element in collection of similar elements. 2

Object Access by Document Position HTML elements exposed via JavaScript are often placed in arrays or collections. The order of insertion into the array is based upon the position in the document. For example, the first tag would be in document.forms[0], the second document.forms[1] and so on. 3

More…. Within the form we find a collection of elements[ ] with the first, or other form field in document.forms[0].elements[0] and so on. As arrays we can use the length property to see how many items are in the page.The downside of access by position is that if the tag moves the script may break 4

Warning! Various browsers support various version of the DOM. If you use latest version of FF, Chrome or IE things will work as described here. Old browsers (or any other browsers) may not support the DOM described here… 5

First Example This is the best paragraph in the document. It is the best because it doesn't contain any words that have the letter 'e'. If you think it is impossible, load me and find out! b = document.getElementById("bestparagraph"); b.innerHTML="Hi world!"; Told you so 6 innerHTML is an attribute of an object that corresponds to an HTML tag. It’s value is the stuff between the start tag and the end tag. this will be the ID we use to locate the object that represents this paragraph

Using IDs If you assign an ID attribute to all your HTML tags, you can access the objects that correspond to those elements directly. The JavaScript document object supports a method that allows you to get at the object that represents an HTML tag with a specific ID. document.getElementById(“foo”); You must use unique names! 7

Important Note Generally you should not have JavaScript variables with the same name as an ID you use to identify an HTML element. – Some browsers automatically create an variable with the same name as an ID attribute. – Some browsers don't. 8

Collections DOM also supports collections of objects, – in Javascript these collections are represented as arrays of objects. Every HTML element has a childNodes collection. The document object has collections forms, images, and links in addition to childNodes. 9

document.all IE supports the document.all collection, which includes an object for every element (tag) in the document. – this is not supported by the W3C DOM standard and is no longer supported by Netscape/Mozilla. It's best to avoid using document.all, but you are likely to see it used in scripts… 10

Viewing the images collection var txt=""; for (var i=0; i<document.images.length;i++) { image = document.images[i]; name = image.getAttribute("src"); txt = txt + name +" "; } document.writeln("Here are the images found: \n"); document.write(txt); 11 Add this to the bottom of any HTML document dom2.html

childNodes collections childNodes : just immediate descendants (so subelements are not in the collection). Each member of the collection has it's own childNodes collection! You can write a recursive function that can be used to display the structure of a document by looking through the childNodes collections. 12 recursion.html

CSS properties Style properties can be accessed through the DOM. – and can be changed in response to user generated events. document.body.style.backgroundColor="blue"; for (i=0;i<document.childNodes.length;i++) { document.childNodes[i].style.fontFamily= "Courier"; } 13

Element CSS position properties You can also mess with CSS position properties Hi Joe document.getElementById(‘joe’).style.left = 200; 14 dom4.html

DOM Issues How to access objects that correspond to elements of the document. – by ID, or in collections. How to know what the various object attributes are that change document element properties – need to look up the names in a DOM reference. 15