CITS1231 Web Technologies JavaScript and Document Object Model.

Slides:



Advertisements
Similar presentations
Java Script Session1 INTRODUCTION.
Advertisements

JavaScript Objects - DOM CST 200 JavaScript. Objectives Introduce JavaScript objects Introduce Document Object Model Introduce window object Introduce.
Working with Special Effects Creating Rollovers, Menus, Filters, and Transitions.
The Web Warrior Guide to Web Design Technologies
Tutorial 16 Working with Dynamic Content and Styles.
Chapter 16 Dynamic HTML and Animation The Web Warrior Guide to Web Design Technologies.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic DHTML.
Chapter 7: Dynamic HTML and Animation JavaScript - Introductory.
Tutorial 13 Working with Objects and Styles. XP Objectives Learn about objects and the document object model Reference documents objects by ID, name,
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.
4.1 JavaScript Introduction
Server vs Client-side validation. JavaScript JavaScript is an object-based language. JavaScript is based on manipulating objects by modifying an object’s.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.
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.
XP New Perspectives on XML, 2 nd Edition Tutorial 10 1 WORKING WITH THE DOCUMENT OBJECT MODEL TUTORIAL 10.
SEG3210 DHTML Tutorial. DHTML DHTML is a combination of technologies used to create dynamic and interactive Web sites. –HTML - For creating text and image.
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.
Lesson 19. JavaScript errors Since JavaScript is an interpreted language, syntax errors will usually cause the script to fail. Both browsers will provide.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming DOM.
DHTML Positioning and Layout. What is DHTML? HTML and xHTML CSS JavaScript or VBScript.
SEG3210 DHTML Tutorial. DHTML DHTML is a combination of technologies used to create dynamic and interactive Web sites. –HTML - For creating text and image.
Creating an Animated Web Page
Working with Objects Creating a Dynamic Web Page.
XP Tutorial 4 New Perspectives on JavaScript, Comprehensive1 Working with Objects Creating an Animated Web Page.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Lesson13. JavaScript JavaScript is an interpreted language, designed to function within a web browser. It can also be used on the server.
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.
XP Tutorial 12 New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with Special Effects Creating Rollovers, Menus, Filters, and Transitions.
DHTML - Introduction Chapter Introduction to DHTML, the DOM, JS review.
 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing.
DHTML: Working with Objects Creating a Dynamic Web Page.
Chapter 7: DHTML: Object Model and Collections CIS 275—Web Application Development for Business I.
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.
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.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 10.
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.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming DHTML Example.
TOPIC II Dynamic HTML Prepared by: Nimcan Cabd Cali.
XP Tutorial 5 New Perspectives on JavaScript, Comprehensive1 Working with Special Effects Creating Rollovers, Menus, Filters, and Transitions.
Lesson 30: JavaScript and DHTML Fundamentals. Objectives Define and contrast client-side and server-side technologies used to create dynamic content for.
JavaScript Object Model. Biggest Advantage of JavaScript  I can access values of HTML elements using JavaScript  I can modify values of HTML elements.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 15 – Dynamic HTML: Object Model and Collections Outline 15.1Introduction 15.2Object Referencing.
Chapter 10 Dynamic HTML (DHTML) JavaScript, Third Edition.
JavaScript IV Robin Burke ECT 270. Outline Final project reminder Style review Manipulating style Special effect examples.
1 Objects In JavaScript. 2 Types of Object in JavaScript Built-in objects User Defined Objects Browser Object Document Object Model.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
The Web Wizard’s Guide To DHTML and CSS Chapter 2 A Review of CSS2 and JavaScript.
XP Tutorial 10 New Perspectives on JavaScript, Comprehensive 1 Working with Dynamic Content and Styles Creating a Dynamic Table of Contents.
Chapter 13: DHTML: Object Model and Collections CIS 275—Web Application Development for Business I.
COM621: Advanced Interactive Web Development Lecture 6 – JavaScript (cont.)
DHTML.
Web Basics: HTML/CSS/JavaScript What are they?
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Introduction to Programming the WWW I
DHTML Javascript Internet Technology.
Chapter 13 - Dynamic HTML: Object Model and Collections
DHTML Javascript Internet Technology.
Introduction to Programming the WWW I
Introduction to DHTML, the DOM, JS review
Presentation transcript:

CITS1231 Web Technologies JavaScript and Document Object Model

2 Objectives Define DHTML and describe its uses Understand objects, properties, methods, and the document object model Work with object references and object collections Modify an object’s properties Apply a method to an object Work with the style object to change the styles associated with an object Work with the properties of the display window Create customized objects, properties, and methods

3 What’s DHTML After HTML, developers began to look for ways to create dynamic pages New approach, in which the HTML code itself supported dynamic elements Known collectively as dynamic HTML, or DHTML Interaction of three aspects –A page’s HTML/XHTML code –A style sheet that defines the styles used in the page –A script to control the behavior of elements on the page

4 DHTML/client-side programming Some uses –Animated text –Pop-up menus –Rollovers –Web pages that retrieve their content from external data sources –Elements that can be dragged and dropped –Simple and quick checks on user filling in form

5 Understanding JavaScript Objects JavaScript is an object-based language An object is any item associated with a Web page or Web browser Each object has –Properties (or attributes) –Methods (or behaviours) which can change the values of properties, or have other effects

6 Document Object Model The organized structure of objects and events is called the document object model, or DOM Every object related to documents or to browsers should be part of the document object model In practice, browsers differ in the objects that their document object models support –Code should be compatible with Netscape 4 Internet Explorer 5 W3C DOM Level 1 and 2 –See compatibility matrix.compatibility matrix

7 Exploring the Document Object Model The document tree

8 Objects Names Each object is identified by an object name

9 Referencing Objects General form is object1.object2.object3… For the body, you would use document.body To reference the history you would use the form window.history Special case: window object is the root object and you can leave out the name window. So in previous example, you can use the form history

10 Working with Object Collections Objects are organized into arrays called object collections

11 Using Collections The object collections are arrays of objects. document.links[0] //the first link on the page. document.links[1] //the second link The length property gives you the number in the collection. Eg, document.links.length is the number of links For example, for( var i=0; i<document.links.length; i++) { do something with document.links[i]; }

12 Referencing Objects Using document.all and document.getElementById –Not all elements are associated with an object collection –Can reference these objects using their id values document.all[ “ id ” ] document.all.id document.getElementById( “ id ” ) id

13 Referencing Objects - Example Hello var x1=document.all["myId"]; var x2=document.all.myId; var x3=document.getElementById("myId"); var x4=myId; alert(x1.innerHTML+x2.innerHTML+x3.innerHTML+x4.innerHTML);

14 Referencing Objects Referencing Tags (eg p, img, table) –Internet Explorer DOM document.all.tags(tag) document.all.tags(p)[0] –W3C DOMs document.getElementsbyTagName(tag) document.getElementsbyTagName( “ p ” )[0] –See compatibility matrix.compatibility matrix

15 Working with Object Properties The syntax for setting the value of an object property is object.property = expression Example document.title = “ Avalon Books ”

16 Working with Object Properties

17 Working with Object Properties Some properties are read-only

18 Working with Object Properties Storing a Property in a Variable variable = object.property Using Properties in a Conditional Expressions if(document.bgColor== “ black ” ) { document.fgColor= “ white ” } else { document.fgColor= “ black ” }

19 Working with Object Methods object.method(parameters)

20 Cross-Browser Web Sites Different browsers support different DOMs. In the real world (not 1231) you may need to accommodate such differences You can create this kind of code, known as cross-browser code, using three different approaches: 1) Using Browser Detection your code determines which browser (and browser version) a user is running. navigator.appName gives name but exact version is hard to get. 2) Object detection means determining which DOM is used by testing which object references are recognized. 3) Common third approach is to use an API which the web browser asks for a page to be constructed from your data.

21 Cross-Browser Code - Example A typical example is CSS for Internet Explorer (IE) and Netscape Navigator 4 (NN4). IE and NN4 reference element styles differently: blah alert(myId.style.color); blah alert(document.ids[myId'].color); This works in IE, not in NN4This works in NN4, not in IE

22 Cross-Browser Code - Example Following example uses navigator.appName and conditional statements to choose correct way to reference an element’s style. Note navigator.appName returns “Microsoft Internet Explorer” for IE, and “Netscape” for NN4. var M=false; var N=false; app=navigator.appName.substring(0,1); if (app=='N') N=true; else M=true; function go() { if (M) alert(myId.style.color); if (N) alert(document.ids[‘myId'].color); } This is Red App = “M” for IE App = “N” for NN4 IE browser only NN4 browser only

23 Working with the style Object The syntax for applying a style is object.style.attribute = value

24 Working with the style Object Setting an Element’s Position

25 Using Path Animation By constantly resetting the position of an object on a web page we can make simple animations.

26 Moving an element Following code from the reference book places an object at a specified location. function placeIt(id, x, y){ object=document.getElementById(id); object.style.left=x+ ” px ” ; object.style.top=y+ ” px ” ; }

27 Your own Objects This lecture: we worked with JavaScript’s built-in objects. Be aware that as in other Object-oriented programming languages, you can create your own classes of objects as well ( but we won’t expect you to do this in CITS1231 ). The programmer has to define (via “function”) what properties and methods the objects in your new class have. S/he can then create many instances of such Objects. Eg, collections of data, arrangements of screen items