Set 2: DOM IT452 Advanced Web and Internet Systems.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Introduction to JavaScript Module 1 Client Side JS Programming M-GO Sponsored By
 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 CLIENT-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes.
WaveMaker Visual AJAX Studio 4.0 Training Troubleshooting.
JS: Document Object Model (DOM)
A really fairly simple guide to: mobile browser-based application development (part 4, JQuery & DOM) Chris Greenhalgh G54UBI / Chris Greenhalgh.
JavaScript & DOM Client-side scripting. JavaScript JavaScript is a tool to automate client side (which is implemented using HTML so far) JavaSript syntax.
Lesson 19. JavaScript errors Since JavaScript is an interpreted language, syntax errors will usually cause the script to fail. Both browsers will provide.
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.
DHTML: Dynamic HTML Internet Technology1. What is DHTML? A collection of enhancements to HTML ► To create dynamic and interactive websites Combination.
Styling and theming Build campaigns in style. What we'll look at... How a web document is structured How HTML and CSS fit together Tools you will need.
Integrating JavaScript and HTML5 HTML5 & CSS 7 th Edition.
CITS1231 Web Technologies JavaScript and Document Object Model.
Javascript. Outline Introduction Fundamental of JavaScript Javascript events management DOM and Dynamic HTML (DHTML)
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.
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.
1 JavaScript in Context. Server-Side Programming.
Tutorial 10 Programming with JavaScript
Done by: Hanadi Muhsen1 Tutorial 1.  Learn the history of JavaScript  Create a script element  Write text to a Web page with JavaScript  Understand.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 10.
Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. HTML5 Evolving Standards JavaScript.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
CS 174: Web Programming October 12 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
1 Final Review. 2 Final Exam  30% of your grade for the course  December 9 at 7:00 p.m., the regular class time  No makeup exam or alternate times.
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.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
Rich Internet Applications 3. Client JavaScript. Document Object Model (DOM) The DOM, is an interface that allows scripts or programs to access and manipulate.
5 th and 4 th ed: some from chapters 9, 12, 13 SY306 Web and Databases for Cyber Operations Slide Set #8: Dynamic HTML.
LING 408/508: Programming for Linguists Lecture 11 October 5 th.
Event Handling (the right way). A Simple Web Page Events - Summary The web page looks like this:
INFM 603: Information Technology and Organizational Context Jimmy Lin The iSchool University of Maryland Wednesday, February 19, 2014 Session 4: JavaScript.
IN THIS LESSON, WE WILL BECOME FAMILIAR WITH HTML AND BEGIN CREATING A WEB PAGE IN YOUR COMPUTER HTML – the foundation.
JS: Document Object Model (DOM) DOM stands for Document Object Model, and allows programmers generic access to: DOM stands for Document Object Model, and.
Introduction to JQuery COGS 187A – Fall JQuery jQuery is a JavaScript library, and allows us to manipulate HTML and CSS after the page has been.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
Introduction to JavaScript LIS390W1A Web Technologies and Techniques 24 Oct M. Cameron Jones.
Chapter 10 Dynamic HTML (DHTML) JavaScript, Third Edition.
JQUERY AND AJAX
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
1 Objects In JavaScript. 2 Types of Object in JavaScript Built-in objects User Defined Objects Browser Object Document Object Model.
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:
ADMINISTRIVIA. LATES, REGRADES, …  Regrades: no more forms: need to see me in person in my office  Excused absences  If foreseeable, needs to be requested.
Build in Objects In JavaScript, almost "everything" is an object.
Javascript and Dynamic Web Pages: Client Side Processing
HTML Elements with JavaScript and DOM Events
Programming Web Pages with JavaScript
Week 3: Introduction to Javascript
Tutorial 10 Programming with JavaScript
© 2015, Mike Murach & Associates, Inc.
HTML.
DHTML Javascript Internet Technology.
DHTML Javascript Internet Technology.
Introduction to Programming the WWW I
Document Object Model (DOM): Objects and Collections
Web Programming Language
Intro to Web Development First Web Page
[Robert W. Sebesta, “Programming the World Wide Web
Murach's JavaScript and jQuery (3rd Ed.)
Web Programming and Design
Web Programming and Design
Web Programming and Design
Presentation transcript:

Set 2: DOM IT452 Advanced Web and Internet Systems

Review – DHTML Basics Find the HTML object we want to change var domLink = document.getElementById("linkToAnimal"); Change the object’s: –HTML properties domLink.href = "cat.html"; –CSS properties domLink.style.backgroundColor = "blue";

Simple Paint (ex1.html) – Part 1 IT 452 DOM demo <!-- var currentAction = "red"; function doupdate(curNode ) { curNode.style.backgroundColor = currentAction; } // -->

Simple Paint (ex1.html) – Part 2 IT 452 DOM demo Tools: Set red Set blue thing 1 thing 2 thing 3 thing 4

What does the DOM tree look like?

ex2.html – add insert/delete/edit function doupdate(curNode ) { var parent = curNode.parentNode; if (currentAction == "insert") { newThing = document.createElement("td"); parent.insertBefore(newThing, curNode); newThing.innerHTML = "hello"; } else if (currentAction == "delete") { parent.removeChild(curNode); } else if (currentAction == "edit") { curNode.innerHTML = window.prompt("Enter new text", ""); } else { curNode.style.backgroundColor = currentAction; }

ex3.html – fully functional function doupdateDyn() { doupdate(this); } function doupdate(curNode ) { var parent = curNode.parentNode; if (currentAction == "insert") { newThing = document.createElement("td"); parent.insertBefore(newThing, curNode); newThing.innerHTML = "hello"; newThing.onclick = doupdateDyn; } else if (currentAction == "delete") { parent.removeChild(curNode); } else if (currentAction == "edit") { curNode.innerHTML = window.prompt("Enter new text", curNode.innerHTML); } else { curNode.style.backgroundColor = currentAction; }

Event Variables Require Event Types 1.Javascript is weakly typed 2.A given variable can be assigned strings, ints, events, etc. 3.However, variables can be assumed to be a certain type, used later by other code. newThing.href assumes a string newThing.onclick assumes an event If you violate these assumptions, something will likely break later on your webpage.

Creating an onclick event for a node. Bad Ideas newThing.onclick = "doupdate(this)“; newThing.onclick = doupdateDyn(); Good Ideas newThing.onclick = doupdateDyn;

Three Ways to do this newThing.onclick = doupdateDyn; newThing.addEventListener(‘click’, doupdateDyn); newThing.onclick = function() { doupdate(this); }

ex4.html – more elegant / alternatives function doupdate(curNode ) { var parent = curNode.parentNode; if (currentAction == "insert") { newThing = document.createElement("td"); parent.insertBefore(newThing, curNode); newThing.innerHTML = "hello"; newThing.onclick = function() {doupdate(this)}; } else if (currentAction == "delete") { parent.removeChild(curNode); } else if (currentAction == "edit") { curNode.firstChild.nodeValue = window.prompt("Enter new text", curNode.innerHTML); } else { curNode.style.backgroundColor = currentAction; }

Summary Grab HTML elements document.getElementById(“yourid”) document.getElementsByTagName(“p”) node.childNodes node.parentNode Create new HTML elements node = document.createElement(“p”) node = document.createTextNode(“text that goes in a p later”) Add new HTML elements to the page node.appendChild(newnode) node.insertBefore(newnode, node) Change HTML attributes/style yourvar.href = “somelink.html” yourvar.style.styleName = “style-val”

Tips Remember JavaScript console How many children? –Use DOM Inspector to see (Firefox and Chrome) –From ex5.html: else if (currentAction == "parent") { var kids = parent.childNodes; window.alert("Number children: "+kids.length); window.alert("Type of first: "+kids[0].nodeType); } See DOM reference links under IT452 page –Use the API!

Ex6.html: iterating over children var kids = parent.childNodes; for (var i=0; i < kids.length; i++) { var theKid = kids[i]; window.alert("Kid #“ + i + " has type“ + theKid.nodeType); }