The Document Object Model (DOM) is a W3C standard.

Slides:



Advertisements
Similar presentations
Chapter 7 JavaScript: Introduction to Scripting. Outline Simple Programs Objects and Variables Obtaining User Input with prompt Dialogs – –Dynamic Welcome.
Advertisements

Working with Forms. how are forms manipulated? the document object contains an array of forms objects, one for each form, in document order –forms[] any.
Sue Wills July Objects The JavaScript language is completely centered around objects, and because of this, it is known as an Object Oriented Programming.
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.
CM143 - Web Week 2 Basic HTML. Links and Image Tags.
Copyright Penny McIntire, Scripting Languages and the DOM.
Web Development & Design Foundations with XHTML Chapter 9 Key Concepts.
1 Web Developer & Design Foundations with XHTML Chapter 6 Key Concepts.
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications1.
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.
Manipulating the DOM CST 200 – JavaScript 3 –
Lesson13. JavaScript JavaScript is an interpreted language, designed to function within a web browser. It can also be used on the server.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Javascript II DOM & JSON. In an effort to create increasingly interactive experiences on the web, programmers wanted access to the functionality of browsers.
Introduction to JavaScript 41 Introduction to Programming the WWW I CMSC Winter 2004 Lecture 17.
JavaScript - A Web Script Language Fred Durao
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 10.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Scripting and Interactivity 이병희
Introduction to JavaScript Objects, Properties, Methods.
HTML JAVASCRIPT. CONTENTS Javascript Example NOSCRIPT Tag Advantages Summary Exercise.
HTML Overview Part 5 – JavaScript 1. Scripts 2  Scripts are used to add dynamic content to a web page.  Scripts consist of a list of commands that execute.
Introduction to HTML. _______________________________________________________________________________________________________________ 2 Outline Key issues.
HTML CS 105. Page Structure HTML elements control the details of how a page gets displayed. Every HTML document has the following basic structure: … …
HTML DOM Basharat Mahmood, Department of Computer Science, CIIT, Islamabad, Pakistan 1.
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
COM621: Advanced Interactive Web Development Lecture 6 – JavaScript (cont.)
Tarik Booker CS 120 California State University, Los Angeles.
REEM ALMOTIRI Information Technology Department Majmaah University.
DHTML.
Introduction to HTML.
Web Basics: HTML/CSS/JavaScript What are they?
Event-Driven Programming
Javascript and Dynamic Web Pages: Client Side Processing
Section 4.1 Section 4.2 Format HTML tags Identify HTML guidelines
JavaScript and HTML Simple Event Handling 11-May-18.
In this session, you will learn to:
Using DHTML to Enhance Web Pages
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Unit M Programming Web Pages with
Chapter 1 Introduction to HTML.
JavaScript is a programming language designed for Web pages.
Key concepts of Computing
Intro to JavaScript CS 1150 Spring 2017.
Section 17.1 Section 17.2 Add an audio file using HTML
JavaScript: Functions
JavaScript Functions.
Scripting Languages and the DOM
Document Object Model (DOM): Objects and Collections
JavaScript and HTML Simple Event Handling 19-Sep-18.
Week 11 Web site: XML DOM Week 11 Web site:
Document Object Model That’s DOM to you
DHTML Javascript Internet Technology.
JQuery UI Plug-ins, Object Models & the Window Object
Chapter 27 WWW and HTTP.
DHTML Javascript Internet Technology.
JavaScript Programming Labs
Chapter 16 The World Wide Web.
Objects An object is a collection of data types as well as functions in one package The various data types called properties and functions called methods.
Introduction to HTML.
Computer communications
© Hugh McCabe 2000 Web Authoring Lecture 12
JavaScript Basics What is JavaScript?
Chapter 7 Event-Driven Pages
JavaScript is a scripting language designed for Web pages by Netscape.
Introduction to JavaScript
JavaScript and HTML Simple Event Handling 26-Aug-19.
JavaScript and HTML Simple Event Handling 4-Oct-19.
Presentation transcript:

The Document Object Model (DOM) is a W3C standard. Internet Programming Chapter 4: Document Object Model (DOM) and JavaScript Objects The Document Object Model (DOM) is a W3C standard. DOM is a platform-independent and language-independent interface that allows programs and scripts to dynamically access and update the content, structure and style of documents. We can use DOM to map HTML into objects for manipulation by general-purpose scripting languages such as JavaScript.

Objects Underneath everything in JavaScript are objects. An object is a collection of data types as well as functions in one package The various data types called properties and functions called methods are accessed using a dot notation. ObjectName.propertyName We have actually been using these ideas already, for example document.write("hello") says using the document object invoke the write() method and give it the string "hello" this results in output to the string

Objects and Methods Many JavaScript objects have methods, which perform some operation when called. We write a method call in a script like this: object.method( ) Methods often have arguments - data that is passed to the method, and used by the method in some way. Examples: window.alert(message) - display message in pop-up box window.close() - close the window document.write(value) - append data to a document document.writeln(value) - append data + newline to a document

JavaScript Objects Objects in JavaScript fall into 2 groups: 1)   User-defined objects These objects are custom objects created by the programmer to bring structure and consistency to a particular programming task.   2)   Built-in objects These objects are provided by the JavaScript language itself. Include those objects associated with primitive data types (String, Number, and Boolean), objects that allow creation of user-defined objects and composite types (Object and Array), and objects that simplify common tasks, such as Date, Math.

DOM in HTML DOM is an evolving standard that specifies how a scripting language can access and manipulate the detailed structure of an HTML document. When an HTML page loads into a scriptable browser, the browser creates a hidden, internal roadmap of ALL the elements it recognizes as scriptable objects The roadmap is a class hierarchy "Document objects" are scriptable objects in a page that can be addressed and moved around.

The DOM has three types of pre-defined objects: DOM Objects The DOM has three types of pre-defined objects: "Utility" objects, such as user-defined objects, String and Math. Browser interaction objects, such as Window, Document, Location. Dynamically-built objects which have a one-to-one relationship with major HTML tags, such as <image> or <table>.

Browser Interaction Objects To understand browser interaction objects, you must understand the object containment hierarchy: The most global (highest-level) object in the DOM is the Window Object. That, in turn, contains the Document Object that represents the HTML document. The document, in turn, may contain a Form Object, which in turn contains form elements…

DOM Hierarchy e.g. <area> in image map <a>

Object Hierarchy in HTML

The Document Object contains: Individual properties, such as the URL of the current document. title. background color of the document. … Properties that are stored as arrays, representing things such as: forms images links Methods, such as Writing text to a document as it loads.

Tag-Associated Objects Under Document When a page loads, objects are built dynamically and have a one-to-one relationship with major HTML tags, such as <image>, <table>, <form>, etc. Attributes from individual HTML tags must be associated with their DOM objects…

Referencing an Object using JavaScript Next, we need to understand the way that JavaScript can access DOM objects... Remember how in HTML many tags had the NAME attribute? <input name ="firstName" type ="text"…>

Referencing an Object using JavaScript We can now use those names to access those objects through the DOM hierarchy. That is, you have to drill down the path through the DOM hierarchy in order to get to the object, just like you have to list upper-level subdirectories to get to a lower-level subdirectory on a computer disk.

Window Document (HTML file) window document image form text field button anchor image form text field text field button anchor

Referencing an Object using JavaScript So, if we have the following (incomplete) HTML code: <body ...> . . . <form name = "userInfo" ...> <input name = "zipCode" type = "text" value = "60115"> </form> <body>

Referencing an Object using JavaScript To access the <form> named userInfo, list each object going down through the hierarchy, separating the various elements with periods: window.document.userInfo Document object, contains... Form object Window object, contains...

Referencing an Object using JavaScript To access the <input> named zipCode: window.document.userInfo.zipCode Window object, contains... Document object, contains... Form object, contains... Form element

Referencing an Object using JavaScript Anyway, the window can usually be omitted: window.document.userInfo.zipCode becomes just document.userInfo.zipCode

Referencing an Object using JavaScript A reference like this gets you to the field, but it won’t get you to the value property, which is the content that the field holds. <input name = "zipCode" type = "text" value = "60115">

Referencing an Object using JavaScript To reference the contents of the text field, specify the value property of that field: document.userInfo.zipCode.value This, then, contains the value currently associated with the zipCode input tag.

JavaScript Objects Thus JavaScript organizes all elements on a web-page in a hierarchy. Every element is seen as an object. Each object can have certain properties and methods. With the help of JavaScript you can easily manipulate the objects. It is very important to understand the hierarchy of HTML-objects. The following code is a simple HTML-page.

Example Accessing HTML Elements with Javascript

The Example Web Page in HTML <body bgcolor="#ffffff"> <center> <img src="home1.gif" id="pic1" name="pic1" /> </center><br /> <form name="myForm"> Name:<input type="text" name="name" id="name" value=""><br/> e-Mail:<input type="text" name="email” value=""><br/><br/> <input type="button" value="Push me" name="myButton" onClick="alert('Hello');"> </form><br /> <img src="ruler1.gif" name="ruler" width="300" height="15" /> <br /><br /> <a href="http://www.vtc.edu.hk/" id="anchor">My homepage</a> </body>

Some ways to access the first image: Accessing the image Some ways to access the first image: - document.pic1 - document.images[0] - document.getElementById("pic1") Some properties for an image: - width - height - src - document.pic1.src = "home2.gif"; - document.images[0].height = 10; - document.getElementById("pic1").width = 20;

Accessing the form elements Some ways to access the textbox: - document.myForm.name.value = "1"; - document.myForm.elements[0].value="2"; - document.getElementById("name").value="3"; Some ways to access the button: - document.myForm.myButton.width=300; - document.myForm.elements[2].height=100;

Accessing the hyperlink Some ways to access the hyperlink (anchor): - document.links[0].href = "http://hk.yahoo.com"; - document.getElementById("anchor").innerHTML = "Yahoo";