Manipulating the DOM CST 200 – JavaScript 3 – 4 - 13.

Slides:



Advertisements
Similar presentations
Microsoft Expression Web-Illustrated Unit K: Working with Behaviors.
Advertisements

Learning HTML. > Title of page This is my first homepage. Tells Browser This is an HTML page Basic Tags Tells Browser End of HTML page Header information.
CHAPTER 7 STYLING CONTENT WITH CASCADING STYLE SHEETS.
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.
JavaScript Objects - DOM CST 200 JavaScript. Objectives Introduce JavaScript objects Introduce Document Object Model Introduce window object Introduce.
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
HTML 5 and CSS 3, Illustrated Complete Unit L: Programming Web Pages with JavaScript.
Lesson 12- Unit L Programming Web Pages with JavaScript.
1 Lesson 10 Using JavaScript with Styles HTML and JavaScript BASICS, 4 th Edition Barksdale / Turner.
Chapter 3 Tables and Page Layout
Web-based Application Development Lecture 9 February 7, 2006 Anita Raja.
Chapter 16 Dynamic HTML and Animation The Web Warrior Guide to Web Design Technologies.
Javascript Document Object Model. How to use JavaScript  JavaScript can be embedded into your html pages in a couple of ways  in elements in both and.
CM143 - Web Week 2 Basic HTML. Links and Image Tags.
JavaScript, Third Edition
Chapter 9 Introduction to the Document Object Model (DOM) JavaScript, Third Edition.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic DHTML.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
Chapter 3 Dreamweaver: Part I The Web Warrior Guide to Web Design Technologies.
_______________________________________________________________________________________________________________ Web Design in a Nutshell 2 nd Edition1.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
Unobtrusive JavaScript
JavaScript 1. What is JavaScript? JavaScript allows web authors to create dynamic pages that react to user interaction. It is an Object-based because.
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.
CSS Class 7 Add JavaScript to your page Add event handlers Validate a form Open a new window Hide and show elements Swap images Debug JavaScript.
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.
Integrating JavaScript and HTML5 HTML5 & CSS 7 th Edition.
CITS1231 Web Technologies JavaScript and Document Object Model.
JavaScript, Fourth Edition
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.
XP Tutorial 12 New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with Special Effects Creating Rollovers, Menus, Filters, and Transitions.
Week 1 – Beginners Content McAfee & Big Fish Games CoderDojo.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
Introducing Cascading Style Sheets. Cascading Style Sheet Basics  Cascading Style Sheet Basics  Creating Styles  Using Styles  Manipulating Styles.
JavaScript, Fourth Edition Chapter 4 Manipulating the Browser Object Model.
Web Design (8) Images (2). My Holiday Photos An exercise in adding and linking images. Create a new website folder calling it ‘My Holiday Photos’. In.
JavaScript. JavaScript is the programming language of HTML and the Web. Easy to learn One of the 3 languages of all developers MUST learn: 1. HTML to.
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 –
HTML JAVASCRIPT. CONTENTS Javascript Example NOSCRIPT Tag Advantages Summary Exercise.
HTML IMAGES. CONTENTS IMG Tag Alt Attribute Setting Width and Height Of An Image Summary Exercise.
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.
Javascript Overview. What is Javascript? May be one of the most popular programming languages ever Runs in the browser, not on the server All modern browsers.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
XP Tutorial 5 New Perspectives on JavaScript, Comprehensive1 Working with Special Effects Creating Rollovers, Menus, Filters, and Transitions.
Understanding JavaScript and Coding Essentials Lesson 8.
Document Object Model Nasrullah. DOM When a page is loaded,browser creates a Document Object Model of the Page.
HTML DOM Basharat Mahmood, Department of Computer Science, CIIT, Islamabad, Pakistan 1.
JavaScript Document Object Model 1. Document Object Model (DOM) JavaScript access to the elements of an HTML document. An object hierarchy Provides access.
Adding Javascript How to include javascript in a webpage.
Project 3: Understanding the JavaScript Object Model Essentials for Design JavaScript Level One Michael Brooks.
Project 7: Exploring DHTML Essentials for Design JavaScript Level Two Michael Brooks.
Chapter 10 Dynamic HTML (DHTML) JavaScript, Third Edition.
Internet & World Wide Web How to Program, 5/e.  JavaScript events  allow scripts to respond to user interactions and modify the page accordingly  Events.
COM621: Advanced Interactive Web Development Lecture 6 – JavaScript (cont.)
Java Script and the DOM DOM stands for: –The Document Object Model When a browser reads a web page, it converts it’s contents from HTML into a hierarchical.
Introduction to.
Programming Web Pages with JavaScript
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
CS 371 Web Application Programming
Intro to Dreamweaver Web Design Section 8-1
Intro to JavaScript CS 1150 Spring 2017.
The Document Object Model (DOM) is a W3C standard.
JQuery UI Plug-ins, Object Models & the Window Object
© 2015, Mike Murach & Associates, Inc.
Presentation transcript:

Manipulating the DOM CST 200 – JavaScript 3 –

Objectives Learn how to work with the Document Object Model (DOM) Learn about the various arrays stored within the Document object Work with the document.images[] Array Use the document.getElementById() function Work with the innerHTML property 2

Document Object Model The Document Object Model (DOM) is a hierarchy of objects that represent the web browser, the web page document and web page elements The DOM provides programmatic access to different aspects of the Web browser window and Web page 3

4 Document Object Model

Document Object Model (cont) Objects in the Document Object Model (DOM) are created automatically by the web browser when the web browser opens a page The top-level object is the Window object, which represents the browser window 5

Window Object The Window object represents a Web browser window The Window object is called the global object because all other DOM objects are contained inside of it We've used the Window object to display pop-up boxes to the user window.alert(msg); 6

The Document Object The Document object represents the Web page displayed in a browser The Document objects contains all the Web page elements – Each web page element is further represented as its own object We've used the Document object to write information to the web page document.write(msg); 7

Document Object Arrays The Document object contains arrays to store and organize specific html elements Each array stores a specific category of html elements These arrays include: – anchors[ ] – applets[ ] – forms[ ] – elements[ ] – images[ ] – links[ ] 8 the images[ ] array stores Image objects corresponding to each element

Accessing DOM Objects Consider the page below, containing one element and three elements: Letters 9

Accessing DOM Objects (cont) When the web browser loads the page, each a new Image object will be created to represent each element The document.images[] array will store all of the Image objects document.images[0] document.images[1] document.images[2] 10

Accessing DOM Objects (cont) The first element of the document.images[] array is an Image object document.images[0] This Image object has properties and methods that can be accessed 11

Accessing Object Properties Once identifying a specific object, we can then access specific properties document.images[0] is the first Image object We can access properties of the image, such as document.images[0].src document.images[0].width document.images[0].height 12 src = "A.jpg" width = 240 height = 240

Web Console Exercise #1 Open the following page in Firefox: Enter the following expressions into Web Console. What does each statement return? document.images[0] document.images[0].src document.images[0].width document.images[0].border document.images[1] document.images[1].height 13

Accessing DOM Objects (cont) Another way of accessing DOM objects is by assigning the name attribute to html elements Below we set the name attribute to the images: 14

Accessing DOM Objects (cont) Elements can then be referenced by adding the name following a period(.), following the document object document.imgA document.imgB document.imgC 15

Accessing DOM Objects (cont) Using the DOM, we can access the same Image object, in two different ways: Through the element's name: document.imgA or through the images[ ] array document.images[0] 16

Modifying Object Properties We can also modify the object properties by assigning a new value to a property // set a new file as the image src document.images[0].src = "newA.jpg"; // set a new image width document.images[0].width = 500; // set a new border width document.imgA.border = 5; // hide image by changing the css display // property document.imgA.style.display="none" 17

Web Console Exercise #2 Return to the letters.html web page in Web Console Enter the following statements into Web Console. What does each statement do? document.imgA.src = "B.jpg" document.imgA.width = 50 document.imgA.width = 350 document.imgA.border = 15 document.imgA.src = "A.jpg" document.imgB.style.display = "none" 18

Accessing DOM Object - Exercise Assume the following page: Welcome Guests Guests can check in anytime after 8pm Write down how to access the creek Image object through the images[ ] array and through the element name. 19

Accessing JavaScript Objects (cont) So far, we have accessed objects through the Document object and images[] array When referring to the current object, another way of accessing an object is through the this keyword – The this keyword refers to the current object Example: 20

Accessing JavaScript Objects (cont) Using the this keyword, we can easily manipulate the current object 21 <img src="A.jpg" alt="A" onmousedown="this.src='B.jpg'" onmouseup="this.src='A.jpg'" /> <img src="B.jpg" alt="B" onmouseover="this.border=20" onmouseout="this.border=0" />

getElementById() function We've seen two ways of accessing a DOM object through the document array and with the name attribute value Another way of accessing a specific DOM object is with the document.getElementById() function JavaScript, Fifth Edition22

getElementById() function (cont) The document.getElementById( ) function returns a DOM object based on the id attribute – In order to use this function, the html element must have been assigned an id attribute JavaScript, Fifth Edition23

getElementById() example Example: imagine in our html document we defined the following element: We can then use the JavaScript expression: document.getElementById("john") to access the DOM object representing the element JavaScript, Fifth Edition24

getElementById() example #2 Intro Welcome to the … JavaScript, Fifth Edition25 document.getElementById("top") document.getElementById("main") document.getElementById("welcome") document.getElementById("footer")

Web Console Exercise #3 Return to the letters.html web page in Web Console Enter the following statements into Web Console. What does each statement do? document.getElementById("letters") document.getElementById("summary") document.getElementById("img5") document.getElementById("img5").width 26

innerHTML property Each html element has an innerHTML property that can be used to change the content of the html element Setting the innerHTML property to a value, will update the content of the html element We will use innerHTML in conjunction with document.getElementById() JavaScript, Fifth Edition27

innerHTML property (cont) Example: imagine in our html document we defined the following element: Welcome to Bill’s Auto We can then use the JavaScript expression: document.getElementById("welcome").innerHTML to access the content of the paragraph element JavaScript, Fifth Edition28

Web Console Exercise #3 Return to the letters.html web page in Web Console Enter the following statements into Web Console. What does each statement do? document.getElementById("summary").innerHTML= “Welcome to Letters Page” document.getElementById("contact").innerHTML= “Contact Us “ 29

Summary Document object model (DOM) is a hierarchy of objects representing web page elements Document object represents the web page, and contains all web page elements Document object contains arrays that can be used to identify Web page elements JavaScript can reference any Web page element through the DOM JavaScript, Fifth Edition30