Lecture 11 – DOM Scripting SFDV3011 – Advanced Web Development Reference: 1.

Slides:



Advertisements
Similar presentations
Sue Wills July Objects The JavaScript language is completely centered around objects, and because of this, it is known as an Object Oriented Programming.
Advertisements

The DOM tree CS The DOM tree CS380 2 Types of DOM nodes  element nodes (HTML tag)  can have children and/or attributes  text nodes (text in.
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.
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
Document Object Model (DOM) JavaScript manipulation of the DOM.
Lesson 12- Unit L Programming Web Pages with JavaScript.
Tutorial 16 Working with Dynamic Content and Styles.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
DHTML - Introduction Introduction to DHTML, the DOM, JS review.
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.
Lecture 2 : Understanding the Document Object Model (DOM) UFCFR Advanced Topics in Web Development II 2014/15 SHAPE Hong Kong.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
JavaScript II ECT 270 Robin Burke. Outline JavaScript review Processing Syntax Events and event handling Form validation.
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.
DOM and JavaScript Aryo Pinandito.
DHTML: Dynamic HTML Internet Technology1. What is DHTML? A collection of enhancements to HTML ► To create dynamic and interactive websites Combination.
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 Objects Creating a Dynamic Web Page.
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.
Javascript II DOM & JSON. In an effort to create increasingly interactive experiences on the web, programmers wanted access to the functionality of browsers.
DHTML - Introduction Chapter Introduction to DHTML, the DOM, JS review.
JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.
XP Tutorial 16 New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with Dynamic Content and Styles Creating a Dynamic Table of Contents.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.
Unleash the Power of jQuery Doncho Minkov Telerik Software Academy academy.telerik.com Senior Technical Trainer
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
IS2802 Introduction to Multimedia Applications for Business Lecture 1: Introduction to IS2802 Rob Gleasure
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
Introduction to the Document Object Model DOM *Understand document structure manipulation *Dynamic effects in web pages *Programming, scripting, web content.
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.2 Revised by Dr. T. Tran for CSI3140.
Unleash the Power of jQuery Learning & Development Team Telerik Software Academy.
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.
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.
TOPIC II Dynamic HTML Prepared by: Nimcan Cabd Cali.
CO1552 – Web Application Development Further JavaScript: Part 1: The Document Object Model Part 2: Functions and Events.
Rich Internet Applications 3. Client JavaScript. Document Object Model (DOM) The DOM, is an interface that allows scripts or programs to access and manipulate.
DOM (Document Object Model) - Parsing and Reading HTML and XML -
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.
Document Object Model (DOM). Outline  Introduction of DOM  Overview of DOM  DOM Relationships  Standard DOM.
Computer Information System Information System California State University Los Angeles Jongwook Woo CIS 461 Web Development I HTML DOM part I Jongwook.
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
XML DOM Week 11 Web site:
Introduction to JavaScript DOM Instructor: Sergey Goldman.
XP Tutorial 10 New Perspectives on JavaScript, Comprehensive 1 Working with Dynamic Content and Styles Creating a Dynamic Table of Contents.
CIS 375—Web App Dev II DHTML. 2 Introduction to DHTML _________ HTML is “a term used by some vendors to describe the combination of HTML, style sheets.
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Handling events from DOM objects.
THE DOM.
DHTML.
Javascript and Dynamic Web Pages: Client Side Processing
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
CGS 3066: Web Programming and Design Spring 2017
JavaScript Event Handling.
Intro to JavaScript CS 1150 Spring 2017.
Document Object Model (DOM): Objects and Collections
Week 11 Web site: XML DOM Week 11 Web site:
DHTML Javascript Internet Technology.
Web Programming A different world! Three main languages/tools No Java
DHTML Javascript Internet Technology.
CHAPTER 7 JavaScripts & HTML Documents
Document Object Model (DOM): Objects and Collections
Introduction to DHTML, the DOM, JS review
Creating dynamic/interactive web pages
Presentation transcript:

Lecture 11 – DOM Scripting SFDV3011 – Advanced Web Development Reference: 1

Document Object Model  JavaScript was added to Netscape Navigator 2 in 1995 – could basically manipulate the browser window  DHTML - Dynamic HTML  in 1996 we got JavaScript 1.1 in Netscape Navigator 3 and IE 3 - could manipulate bits of a document such as images and forms  Netscape Navigator 4 and IE 4 both promised to let you manipulate the whole document - but they did it completely differently  But the W3C created a standard Document Object Model (DOM), which browser manufacturers eventually implemented 2

Document Object Model  “The Document Object Model is a platform- and language- neutral interface that will allow programs and scripts to dynamically access and update the content, structure and style of documents.” – from  DHTML - collection of technologies that enable creation of interactive and animated websites; these include: static HTML CSS DOM JavaScript 3

Basic DOM Concepts  Your document is parsed into a tree-structure  Each part of the tree is a “node” and the usual tree language applies: root, child, parent, sibling, ancestor, descendant  There are separate nodes for elements, attributes, and text  A compliant DOM implementation provides certain expected commands to get node content, change it, add nodes, and remove nodes  In principle the DOM is independent of platform, programming language, and XML application 4

DOM example Page title A header A link 5

DOM example This is DOM diagram of the previous HTML document element element innerHTML: “Page title” element element innerHTML: “A header” element innerHTML: “A link” Attribute: “href” document Root element 6

Manipulating the DOM with JavaScript  JavaScript is a script that is embedded in (or attached to) an HTML document that gets executed on the client machine  JavaScript can manipulate the DOM that has been loaded into the browser from a given HTML page  Ability to manipulate the DOM gives you power to animate the web page 7

Adding JavaScript to HTML var hdrObj = document. getElementById (" hdr1 "); hdrObj. innerHTML = " Changed text "; Header. Index.html 8

Adding JavaScript to HTML Header … Index.html function setup () { var hdrObj = document. getElementById (" hdr1 "); hdrObj. innerHTML = " Changed text "; } if ( document. getElementById ) { // Function setup will be executed after DOM is loaded window. onload = setup ; } Script.js 9

Getting the elements  To perform operation/property change on a given element, you need to first get a reference to the desired element  Reference by id (remember, id should be unique for the entire document) var hdrObj = document. getElementById (" some_id ");  Reference by name  Reference by element type  Traverse the object tree var formObjs = document. getElementsByName (" some_name "); var hdrObjs = document. getElementsByTagName ("h1"); var bodyObj = document. getElementsByTagName (" body "); var hdrObj = bodyObj [0]. firstElementChild ; 10

Creating and deleting elements  Create a new element var listItem = document. createElement ("li");  Insert element into a document tree  Delete element from a document listItem. innerHTML = " Another item "; var list = document. getElementByName (" listA "); list. appendChild ( listItem ); list. removeChild ( listItem ); 11

Changing element properties // Get the reference to all images in the document var imgArray = document. getElementsByTagName ("img"); // Get the reference to the first image in the document var imgObj = imgArray [0]; // Change the image source attribute imgObj. src = " differentImage.jpg "; // Get the reference to an element with id " myform " var frmObj = document. getElementById (" myform "); // Assuming it ’s a form element, set its method to post frmObj. method = " post "; 12

Defining events associated with elements  Event: onclick // Get the reference to all images in the document var imgObj = document. getElementId (" imgA "); // Set the image onclick handler imgObj. onclick = doSomething ; function doSomething () { alert ("You clicked the image !"); }  And many more: onmouseover, onload, onsubmit, onfocus  Note: not every event may be supported in every browser - Check 13

Changing CSS styles  Use the style property of the element object var obj= document. getElementById (" someID "); obj. style. visibility = " visible ";  The style attributes in JavaScript mostly match CSS styles (with few exceptions - cgi/javamanual/javastyle.html) 14

Unobtrusive JavaScript  Separate behaviour (JavaScript) from the structure/content (HTML) and presentation (CSS) in a web page  Take into account browser inconsistencies  And make sure that the functionality degrades gracefully when JavaScript is not enabled (or not supported) 15