CHAPTER 5 DOCUMENT OBJECT MODEL

Slides:



Advertisements
Similar presentations
HTML DOM Part-II. Create Node We can create a node in JavaScript dynamically. There are 3 types of Node –Comment –Element –Text Node We can also create.
Advertisements

The Document Object Model
XML CS What is XML?  XML: a "skeleton" for creating markup languages  you already know it!  syntax is identical to XHTML's: content  languages.
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
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.
Document Object Model (DOM) JavaScript manipulation of the DOM.
JavaScript APIs You’ve Never Heard Of (And some you have) Nicholas C.
HTML 5 and CSS 3, Illustrated Complete Unit L: Programming Web Pages with JavaScript.
Video, audio, embed, iframe, HTML Form
Document Object Model. Lecture 18 The Document Object Model (DOM) is not a programming language It is an object-oriented model of web documents Each.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
Chapter 9 Introduction to the Document Object Model (DOM) JavaScript, Third Edition.
Introduction to the OWASP Top 10. Cross Site Scripting (XSS)  Comes in several flavors:  Stored  Reflective  DOM-Based.
Forms, Validation Week 7 INFM 603. Announcements Try placing today’s example in htdocs (XAMPP). This will allow you to execute examples that rely on PHP.
CST JavaScript Validating Form Data with JavaScript.
1 Forms A form is the usual way that information is gotten from a browser to a server –HTML has tags to create a collection of objects that implement this.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
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.
JavaScript & DOM Client-side scripting. JavaScript JavaScript is a tool to automate client side (which is implemented using HTML so far) JavaSript syntax.
Lecture 11 – DOM Scripting SFDV3011 – Advanced Web Development Reference: 1.
D2L Notes Be sure to submit your link in the dropbox provided on D2L You can just upload an empty text file if a file upload is required Do not use D2L.
Working with the XML Document Object Model ©NIITeXtensible Markup Language/Lesson 7/Slide 1 of 44 Objectives In this lesson, you will learn to: *Identify.
 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.
Client side web programming Introduction Jaana Holvikivi, DSc. School of ICT.
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.
JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.
Navigating the DOM with ExtJS By Aaron Conran. Document Object Model The Document Object Model or DOM is a standard to represent HTML, XHTML and other.
JavaScript III ECT 270 Robin Burke. Outline Form validation Regular expressions DOM JS document model review W3C DOM Cross-browser scripting Style.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
Introduction to the Document Object Model Eugenia Fernandez IUPUI.
Markup basics. Markup languages add commentary to text files –so that the commentary can be ignored if not understood eg HyperText Markup Language –adds.
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 –
XML DOM.
HTML JAVASCRIPT. CONTENTS Javascript Example NOSCRIPT Tag Advantages Summary Exercise.
DOM (Document Object Model) - Parsing and Reading HTML and XML -
School of Computing and Information Systems CS 371 Web Application Programming JavaScript - DOM Modifying the Page from within.
INTRODUCTION JavaScript can make websites more interactive, interesting, and user-friendly.
Create Element, Remove Child. The Document Tree Document Element Root Element Element Element Element Element Text: HelloWorld Attribute “href”
JS: Document Object Model (DOM) DOM stands for Document Object Model, and allows programmers generic access to: DOM stands for Document Object Model, and.
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:
XML DOM Week 11 Web site:
DOM Dr. Reda Salama. Back to the HTML DOM Once we get a response back from the server, we probably want to update our HTML page The HTML page itself is.
Introduction to JavaScript DOM Instructor: Sergey Goldman.
THE DOM.
Introduction to JavaScript DOM and Events
Unit 4 Representing Web Data: XML
>> JavaScript: Document Object Model
Scripting the DOM MIS 3502, Fall 2016 Jeremy Shafer Department of MIS
CGS 3066: Web Programming and Design Spring 2017
Applied Online Programming
CS 371 Web Application Programming
Introduction to the Document Object Model
DOM Robin Burke ECT 360.
CGS 3066: Web Programming and Design Spring 2016
CGS 3066: Web Programming and Design Spring 2017
Document Object Model (DOM): Objects and Collections
Chapter 7 Representing Web Data: XML
Week 11 Web site: XML DOM Week 11 Web site:
In this session, you will learn to:
© 2015, Mike Murach & Associates, Inc.
Document Object Model (DOM): Objects and Collections
Javascript and JQuery SRM DSC.
Creating dynamic/interactive web pages
Presentation transcript:

CHAPTER 5 DOCUMENT OBJECT MODEL

The DOM specifies how:

Browsers create a model of an HTML page The DOM specifies how: 1 Browsers create a model of an HTML page

1 2 The DOM specifies how: Browsers create a model of an HTML page JavaScript accesses / updates an HTML page

THE DOM TREE

<ul> <li></li> <li></li> <li></li> <li></li> </ul>

ELEMENT NODES <ul> <li></li> <li></li> <li></li> <li></li> </ul> ul li

TEXT NODES <ul> <li>fresh figs</li> <li>pine nuts</li> <li>honey</li> <li>balsamic vinegar</li> </ul> ul li text

ATTRIBUTE NODES <ul> <li id=“one” class=“hot”>fresh figs</li> <li id=“two” class=“hot”>pine nuts</li> <li id=“three” class=“hot”>honey</li> <li id=“four”>balsamic vinegar</li> </ul> ul li attribute text

To access and update the HTML, first you select the element(s) you want to work with.

Here are some of the ways ways to select element nodes Here are some of the ways ways to select element nodes. They are known as DOM queries.

DOM QUERIES

getElementById(‘one’); <ul> <li id=“one” class=“hot”>fresh figs</li> <li id=“two” class=“hot”>pine nuts</li> <li id=“three” class=“hot”>honey</li> <li id=“four”>balsamic vinegar</li> </ul> ul li getElementById(‘one’);

getElementsByClassName(‘hot’); <ul> <li id=“one” class=“hot”>fresh figs</li> <li id=“two” class=“hot”>pine nuts</li> <li id=“three” class=“hot”>honey</li> <li id=“four”>balsamic vinegar</li> </ul> ul li getElementsByClassName(‘hot’);

getElementsByTagName(‘li’); <ul> <li id=“one” class=“hot”>fresh figs</li> <li id=“two” class=“hot”>pine nuts</li> <li id=“three” class=“hot”>honey</li> <li id=“four”>balsamic vinegar</li> </ul> ul li getElementsByTagName(‘li’);

querySelector(‘#two’); <ul> <li id=“one” class=“hot”>fresh figs</li> <li id=“two” class=“hot”>pine nuts</li> <li id=“three” class=“hot”>honey</li> <li id=“four”>balsamic vinegar</li> </ul> ul li querySelector(‘#two’);

querySelectorAll(‘li.hot’); <ul> <li id=“one” class=“hot”>fresh figs</li> <li id=“two” class=“hot”>pine nuts</li> <li id=“three” class=“hot”>honey</li> <li id=“four”>balsamic vinegar</li> </ul> ul li querySelectorAll(‘li.hot’);

NODELISTS

If a DOM query returns more than one element, it is known as a NodeList.

Items in a NodeList are numbered and selected like an array: var elements; elements = getElementsByClassName(‘hot’); var firstItem = elements[0]; Items in a NodeList are numbered and selected like an array:

You can check if there are elements before using a NodeList: if (elements.length >= 1) { var firstItem = elements[0]; } You can check if there are elements before using a NodeList:

TRAVERSING THE DOM

You can move from one node to another if it is a relation of it You can move from one node to another if it is a relation of it. This is known as traversing the DOM.

STARTING ELEMENT ul li li

ul li li parentNode

ul li li previousSibling

ul li nextSibling

STARTING ELEMENT ul li li

ul li firstChild

ul li lastChild

WORKING WITH ELEMENTS

Elements can contain: Text nodes Element content Attributes

<li id=“one”>figs</li> attribute text: figs <li id=“one”>figs</li>

<li id=“one”><em>fresh</em> figs</li> attribute text: figs em fresh <li id=“one”><em>fresh</em> figs</li>

<li id=“one”>six <em>fresh</em> figs</li> attribute text: figs em fresh six <li id=“one”>six <em>fresh</em> figs</li>

To access their content you can use: nodeValue on text nodes textContent for text content of elements innerHTML for text and markup

nodeValue works on text nodes li attribute text: figs em fresh var el = document.getElementById(‘one’); el.firstChild.nextSibling.nodeValue; returns: figs

document.getElementById(‘one’).textContent; returns: fresh figs textContent just collects text content li attribute text: figs em fresh document.getElementById(‘one’).textContent; returns: fresh figs

innerHTML gets text and markup li attribute text: figs em fresh document.getElementById(‘one’).innerHTML; returns: <em>fresh</em> figs

DOM MANIPULATION createElement() createTextNode() appendChild() innerHTML Builds up a string Contains markup Updates elements vs

CROSS-SITE SCRIPTING (XSS) ATTACKS

Untrusted data is content you do not have complete control over Untrusted data is content you do not have complete control over. It can contain malicious content.

Sources of untrusted data: User creates a profile Multiple contributors Data from third-party sites Files such as images / videos are uploaded

Validate all input that is sent to the server DEFENDING AGAINST XSS Validate all input that is sent to the server Escape data coming from the server

WORKING WITH ATTRIBUTES

ACCESSING AN ATTRIBUTE 1. Use a DOM query to select an element: var el = document.getElementById(‘one’); 2. Method gets attribute from element: el.getAttribute(‘class’); ACCESSING AN ATTRIBUTE

Check for attribute and update it: var el = document Check for attribute and update it: var el = document.getElementById(‘one’); if (el.hasAttribute(‘class’) { el.setAttribute(‘class’, ‘cool’); } UPDATING AN ATTRIBUTE