The Document Object Model. Goals Understand how a JavaScript can communicate with the web page in which it “lives.” Understand how to use dot notation.

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
Computer and Communication Fundamental Basic web programming Lecture 8 Rina Zviel-Girshin.
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.
The Web Warrior Guide to Web Design Technologies
JavaScript Forms Form Validation Cookies. What JavaScript can do  Control document appearance and content  Control the browser  Interact with user.
Javascript Client-side scripting. Up to now  We've seen a little about how to control  content with HTML  presentation with CSS  Javascript is a language.
Guide To UNIX Using Linux Third Edition
CS 299 – Web Programming and Design Overview of JavaScript and DOM Instructor: Dr. Fang (Daisy) Tang.
CST JavaScript Validating Form Data with JavaScript.
Chapter 9 Collecting Data with Forms. A form on a web page consists of form objects such as text boxes or radio buttons into which users type information.
CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Using Arrays in JavaScript.
4-Sep-15 HTML Forms Mrs. Goins Web Design Class. Parts of a Web Form A Form is an area that can contain Form Control/Elements. Each piece of information.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Chapter 6: Forms JavaScript - Introductory. Previewing the Product Registration Form.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
Arrays – What is it? – Creation – Changing the contents Functions – What is it? – Syntax – How they work – Anonymous functions A quick lesson in Objects.
Robinson_CIS_285_2005 HTML FORMS CIS 285 Winter_2005 Instructor: Mary Robinson.
Tutorial 2 Variables and Objects. Working with Variables and Objects Variables (or identifiers) –Values stored in computer memory locations –Value can.
Chapter 8 Cookies And Security JavaScript, Third Edition.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
CIS 451: ASP.NET Objects Dr. Ralph D. Westfall January, 2009.
Tutorial 10 Programming with JavaScript
Using Client-Side Scripts to Enhance Web Applications 1.
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.
JavaScript, Fourth Edition Chapter 5 Validating Form Data with JavaScript.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Chapter 5: Windows and Frames
JS Basics 1 Lecture JavaScript - Basics. JS Basics 2 What is JavaScript JavaScript is a “simple”, interpreted, programming language with elementary object-
Document Objects Forms, Images and Links. The document object model Each element of an HTML document, such as an image, form, link, or button, can be.
Introducing JavaScript. Goals By the end of this lecture you should … Be able to describe the differences among object methods, object properties and.
Overview of Form and Javascript fundamentals. Brief matching exercise 1. This is the software that allows a user to access and view HTML documents 2.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
XP Tutorial 8 Adding Interactivity with ActionScript.
Scripting and Interactivity 이병희
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
JavaScript, Fourth Edition
1 The Information School of the University of Washington Nov 1fit dom © 2006 University of Washington Document Object Model (DOM)
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
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.
Introduction to JavaScript CSc 2320 Fall 2014 Disclaimer: All words, pictures are adopted from “Simple JavaScript”by Kevin Yank and Cameron Adams and also.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
Scripting with Client-Side Processing Scripting with Client Side Processing Lesson – Web Technologies Copyright © Texas Education Agency, All rights.
Copyright ©2005  Department of Computer & Information Science Multidimensional Arrays.
JavaScript, Sixth Edition
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
CGS 3066: Web Programming and Design Spring 2016 Introduction to JavaScript.
JavaScript and Ajax Week 10 Web site:
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
COM621: Advanced Interactive Web Development Lecture 6 – JavaScript (cont.)
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
Introduction to.
JavaScript, Sixth Edition
Chapter 5 Validating Form Data with JavaScript
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Tutorial 10 Programming with JavaScript
14 A Brief Look at JavaScript and jQuery.
T. Jumana Abu Shmais – AOU - Riyadh
JavaScript What is JavaScript? What can JavaScript do?
JavaScript What is JavaScript? What can JavaScript do?
Introducing JavaScript
Web Programming and Design
Web Programming and Design
Presentation transcript:

The Document Object Model

Goals Understand how a JavaScript can communicate with the web page in which it “lives.” Understand how to use dot notation. Understand how to read form elements. Understand how to work with new windows.

Review – Objects Objects are unique programming entities in scripts or web pages. Objects have properties which describe them; methods (actions that objects can do) and events to which they can react. A property is an object’s characteristic. Typically, properties come in property-value pairs (i.e. - background-color is a property; blue is a value). A method is something that an object can do. An event is an action to which an object can react (i.e. – when a web page loads).

Introducing DOM Describes the way JavaScript (and other client-side languages) can communicate with the web page in which it “lives.” DOM allows us to dynamically update Web pages. Because of DOM, we can now use JavaScript to interact with CSS and other web elements, like form objects.

Introducing DOM DOM relies on interaction with objects in a web page. The document object is considered the “core” object in DOM. The top-level DOM object is the window object. We can dynamically assign values to object properties:

Reading Properties Not only can we change properties dynamically, we can read properties, as well:

Common Document Properties document.bgColor – The background color of the page. document.fgColor – The text color used on a page. document.domain – The address of the web server that houses the page. document.lastModified – The date and the last time someone edited the page.

Common Document Properties document.URL – The address of the web page. document.location – (Deprecated) Same as document.URL document.referrer – The address of the page that linked to the current page. document.title – The text contained in the … tag of the current page.

Common Document Methods document.open() – Begin a new document, replacing existing content (destructive) document.close() – Ends a document begun with the document.open() method document.write() – Append text to the current document document.writeln() – Append text to the current document and include a newline character (line break)

JavaScript Events & DOM Through recording and reacting to events, we make our web page more interactive. We can add this interactivity by detecting events in a document’s environment and writing programming statements to react to those events. Typically, we code this using functions:

Getting Input Using DOM Input is not just text anymore! It can come from form components (text fields, password fields, hidden fields, radio buttons, etc.). We can use dot notation to represent DOM hierarchy. DOM hierarchy provides each form element with its unique “address.”

Dot Notation The unique “address” of an object From L to R, start with the most general, moving to the object itself window.document.myForm.fName.value Browser Window The Current Page Form Name Field Name Value Property

Input using Text Fields Name the form Name all fields Use dot notation to reference specific fields Straight-forward, similar to how we reference variables Other objects that use the same syntax: ◦ textarea ◦ password field ◦ hidden

Input using Check Boxes Name the form Name all fields Use dot notation to reference specific fields Used the “checked” instead of the “value” property Returns a Boolean value: ◦ true ◦ false Use if structure to evaluate

DOM and Arrays For some form objects, we need to use a new type of data structure – an array. What is an array? ◦ An array is a data structure that can hold multiple values (unlike a variable, which can only hold 1 value at a time). ◦ An array is useful when we work with groups of form objects, like radio buttons

Arrays and Memory In a computer’s memory, an array represents multiple contiguous locations that all contain the values of the same data type. We represent each “slot” in an array with a number: ◦ Numbers start at zero. ◦ An element’s position (represented by a number) is called an index (or subscript)

Why use Arrays? Use an array when you need to group values by some common characteristic (e.g. – Students in a classroom). When designing an application that performs the same processing for multiple sets of data, use an array and a looping structure.

Conceptual Example of Arrays 0John 1Mary 2Elizabeth 3Omar 4Charles 5Ravi 6Katherine 7Hannah 8Alexander 9William Array Positions (Elements) – Each number represents the subscript (index) of its corresponding position Array Values – The value stored in contiguous memory cells Array Name = Class

Array Elements Elements populate an array. Each element is a discrete value. We identify elements using a unique index (the element’s “address”). Array index numbering starts with zero and increments by one for each new element.

Declaring an Array To declare an array in JavaScript, we use the array constructor and assign the array to a variable. We give the constructor the size of the array as an argument: var n341Class = new Array(34); At times, we may not know the size of the array when declaring. In such situations, we can declare an empty array: var n341Class = new Array();

Adding Values to an Array To add values to an array, assign the new values while also identifying the index where you want JavaScript to store the new value: n341Class[0] = “Jennifer”; n341Class[1] = “Joseph”; n341Class[2] = “Marcus”; n341Class[3] = “Alex”;

Using a For Loop to Add Values In some instances, we can use a for loop to add values: for(var i=0; i<10; i++) { var newStudent = new String(“”); newStudent = “Student”; newStudent = i.toString(); n341Class[i] = newStudent; }

Parallel (Corresponding) Arrays Parallel arrays gives you the ability to store multiple pieces of information about a single entity in an application. The indexes of each array should correspond to one another for individual entities: ◦ Student ID ◦ Student Name ◦ Student Exam Score

Conceptual Example of Parallel Arrays Array Name = strSID0John1Mary 2Elizabeth 3Omar 4Charles 5Ravi 6Katherine 7Hannah 8Alexander 9William Array Name = strN341Class Array Name = fltExam1Scores

Creating Parallel Arrays To create parallel arrays, just create them as you would any other array: var SID = new Array(); var n341Class = new Array(); var examScr = new Array(); The arrays are separate arrays in memory, but your program should treat them as one …

Assigning Values to Parallel Arrays To assign values for a single entity to parallel arrays, use the same index number for each assignment: SID[5] = “025769”; n341Class[5] = “Ravi”; examScr[5] = 82;

The Array.length Property Just like any JavaScript objects, arrays have properties. One of the most useful properties is the length property, which will give you a count of how many indexes the array has.

The Array.length Property Example of the Array.length property: var golfScores = new Array(); var golfScrSize = 0; golfScores[0] = 72; golfScores[1] = 85; golfScores[2] = 125; //golfScrSize will get the //value of 3 golfScrSize = golfScores.length;

Using Array.length The following code averages the values stored in an array for(var i=0; i<golfScores.length; i++) { sumScores += golfScores[i] } avgScore = sumScores/golfScores.length;

Array Examples Single Array Parallel Arrays

Input using Radio Buttons Name the form. Name the group (each button of the same group has the same name) Assign each button a “value” property. Use an if structure nested in a for loop to evaluate each button.

Input using Selection Objects Similar to radios (use an array to evaluate) No need for a loop because there is only one object rather than several options. Name the form Name the select object Assign each option a “value” property

Outputting to Windows Use a function Create a new window using the open() method of the window object. The open() method takes the following parameters: ◦ URL ◦ Name of the window object ◦ Window properties:  Size  Menus?  Toolbars?

Outputting to Windows - Examples Example 1 Example 2

Questions?

Resources Flanagan, David. JavaScript: The Definitive Guide, 4 th Edition. O’Reilly, 2002.