JavaScript Object Model. Biggest Advantage of JavaScript  I can access values of HTML elements using JavaScript  I can modify values of HTML elements.

Slides:



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

CSS The basics { }. CSS Cascading Style Sheets - language used to – describe html appearance & formatting Style Sheet - file that describes – how html.
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.
1 Web Developer & Design Foundations with XHTML Chapter 9 Key Concepts.
กระบวนวิชา CSS. What is CSS? CSS stands for Cascading Style Sheets Styles define how to display HTML elements Styles were added to HTML 4.0 to.
CSD 340 (Blum)1 Using Microsoft Visual Studio.NET Development Environment and Introducing Functions.
Hypertext Markup Language. Platform: - Independent  This means it can be interpreted on any computer regardless of the hardware or operating system.
1 The Information School of the University of Washington Nov 1fit dom © 2006 University of Washington Document Object Model (DOM) INFO/CSE 100, Fall.
COMP101 – Exploring Multimedia and Internet Computing LA2 (Thur 14:00 – 16:50) TA: Jackie Lo.
Tutorial 16 Working with Dynamic Content and Styles.
CM143 - Web Week 2 Basic HTML. Links and Image Tags.
Computer Science 103 Chapter 4 Advanced JavaScript.
Lecture 13. A Very Brief Introduction to HTML and XHTML, part II Instructor: Jie Yang Department of Computer Science University of Massachusetts Lowell.
HTML Tags. Objectives Know the commonly used HTML tags Create a simple webpage using the HTML tags that will be discussed.
Headings, Paragraphs, Formatting, Links, Head, CSS, Images
Server vs Client-side validation. JavaScript JavaScript is an object-based language. JavaScript is based on manipulating objects by modifying an object’s.
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.
Chapter 2 Developing a Web Page. A web page is composed of two distinct sections: –The head content –The body Creating Head Content and Setting Page Properties.
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.
JavaScript & DOM Client-side scripting. JavaScript JavaScript is a tool to automate client side (which is implemented using HTML so far) JavaSript syntax.
Objectives: 1. Create a Skeleton HTML 2. View a Skeleton File Through a Server and Browser 3. Learn HTML Body Tags for the Display of Text and Graphics.
1 CSC160 Chapter 9: The Document Object. Outline Document objects Properties of the document object linkColor alinkColor vlinkColor bgColor fgColor lastModified.
Creating an Animated Web Page
CITS1231 Web Technologies JavaScript and Document Object Model.
Manipulating the DOM CST 200 – JavaScript 3 –
CSS Basic (cascading style sheets)
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. What is HTML? HTML is a language for describing web pages. HTML stands for Hyper Text Markup Language HTML is not a programming language,
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.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.
 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing.
Chapter 7: DHTML: Object Model and Collections CIS 275—Web Application Development for Business I.
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.
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
Colors & Fonts Building a Website Lesson 7. Font Font The tag specifies the font face, font size, and color of text. The tag can have any or all of these.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
Are You Smarter Than a 5 th Grader? 1,000,000 5th Grade HTML 5th Grade Syntax 4th Grade HTML 4th Grade Syntax 3rd Grade HTML 3rd Grade Syntax 2nd Grade.
WEEK -1 ACM 262 ACM 262 Course Notes. HTML What is HTML? HTML is a language for describing web pages. HTML stands for Hyper Text Markup Language HTML.
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.
School of Computing and Information Systems CS 371 Web Application Programming JavaScript - DOM Modifying the Page from within.
Javascript Overview. What is Javascript? May be one of the most popular programming languages ever Runs in the browser, not on the server All modern browsers.
HTML DOM Basharat Mahmood, Department of Computer Science, CIIT, Islamabad, Pakistan 1.
JavaScript Document Object Model 1. Document Object Model (DOM) JavaScript access to the elements of an HTML document. An object hierarchy Provides access.
HTML-I Basic HTML Elements. HTML (Hyper Text Markup Language) HTML is a document layout and hyperlink- specification language. i.e. a language used to.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 15 – Dynamic HTML: Object Model and Collections Outline 15.1Introduction 15.2Object Referencing.
1 Your Web Page title body of Web page main heading H2 heading bulleted list paragraph.
HTML is the language that allows text and graphics to be displayed as Web pages. It is a set of special codes, called tags, that tells a browser application.
Chapter 13: DHTML: Object Model and Collections CIS 275—Web Application Development for Business I.
COM621: Advanced Interactive Web Development Lecture 6 – JavaScript (cont.)
CASCADING STYLE SHEET CSS. CSS stands for Cascading Style Sheets Styles define how to display HTML elements Styles were added to HTML 4.0 to solve a problem.
1 The Document Object Model. 2 Objectives You will be able to: Describe the structure of the W3C Document Object Model, or DOM. Use JavaScript to access.
Web Basics: HTML/CSS/JavaScript What are they?
>> JavaScript: Document Object Model
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
CS 371 Web Application Programming
Key concepts of Computing
Intro to JavaScript CS 1150 Spring 2017.
CASCADING STYLE SHEET CSS.
Website Design 3
Chapter 13 - Dynamic HTML: Object Model and Collections
Basic HTML and Embed Codes
© 2015, Mike Murach & Associates, Inc.
Intro to Web Development First Web Page
Javascript.
Chengyu Sun California State University, Los Angeles
Murach's JavaScript and jQuery (3rd Ed.)
© 2017, Mike Murach & Associates, Inc.
Presentation transcript:

JavaScript Object Model

Biggest Advantage of JavaScript  I can access values of HTML elements using JavaScript  I can modify values of HTML elements using JavaScript

Disadvantage of JavaScript  Each browser has its own document object Model

Initial JavaScript Object Model

New JavaScript Object Model

New Properties added in Latest JavaScript Model  all[]  Children[]  className  innerHTML  innterText  outerHTML  outerText

New Properties added in Latest JavaScript Model  parentElement  style  tagName

New methods added in Latest JavaScript Model  Click()  getAttribute()  insertAdjacentHTML()  insertAdjacentText()  setAttribute()  removeAttribute()

Difference between Initial Model and New model  Slide No. 7 and 9 shows the new methods and new properties  Slide no.5 blue circles shows the new objects added in Latest JavaScript Model

How to use these Object Model  We have been doing this in our previous lecture, but I will take few more examples now.

Sample Code   Test Document   Sample link   Sample link 2 

How many Forms are there?  Use object forms[]  document.forms[] is an array  document.forms.length returns the number of forms in the web page  document.forms[0] is the first form, OR  document.form1 is the first form  document.forms[0].name returns the name of first form i.e. form1

Code to Access All Forms  if (document.forms.length > 0)  {  document.write(" Forms ");  document.write("# Forms = " + document.forms.length + " ");  for (i=0; i < document.forms.length; i++)  document.write("Forms["+i+"]=" + document.forms[i].name + " ");  }

How many anchors are there?  Use object anchors[]  document.anchors[] is an array  document.anchors.length returns the number of anchors in the web page  document.anchors[0] is the first anchor, OR  document.anchor1 is the first anchor

Code to Access all anchors  if (document.anchors.length > 0)  {  document.write(" Anchors ");  document.write("# Anchors = " + document.anchors.length + " ");  for (i=0; i < document.anchors.length; i++)  document.write("Anchors["+i+"]=" + document.anchors[i] + " ");  }

How many links are there?  Use object links[]  document.links[] is an array  document.links.length returns the number of links in the web page  document.links[0] is the first link, OR  document. name of link is the first link (In our example, we didn’t use any any name for link)

Other Properties  document.bgColor - returns the background color of web page  document.fgColor – returns the foreground color of web page.  document.location – returns the location of web page i.e URL  document.URL – returns the URL of web page  document.title – returns the title of web page.

More properties  document.alinkColor – returns the active link color  document.vlinkColor – returns the visited link color.  document.linkColor – returns the link color.  Slide were based on oldest JavaScript Model.

Sample Code 

Using elements[]  To access 1 st form, I can write document.forms[0]  To access 2 nd form, I can write document.forms[1]  To access 1 st text box inside 2 nd form document.forms[1].elements[0]  To access 2 nd text box inside 1 st form document.forms[1].elements[1]

Using elements[]  elements can be radio button, text box, check box etc. Also called as form elements.  elements[] is defined inside forms[], (Refer slide 4 and 5).  elements.length return the number of elements inside a particular form.  elements[0] refer to 1 st element, elements[1] refer to 2 nd element and so on.

2 nd Method of accessing elements  By using name attribute of elements. (We discussed this with forms and anchors and links)

2 nd method of accessing elements   document.myForm.userName refer to the text box inside form.

3 rd method of accessing elements  Using getElementById()  Element that we want to access must have its id attribute defined

Example of getElementById   this is a paragraph   pid = document.getElementById(“p1”);  p1.align = “right”;

document.getElementById  pid =getElementById(“p1”) Returns the reference of object and store it in pid.  pid.align = “right” This will align the paragraph with id=p1 to the right By Id we can get access to any element in the web page.

document.all[]  document.all[] – return array of all html tags in the web page. i.e. document.all.length is the count of number of tags in web page. document.all[0] returns the reference of 1 st tag. document.all[1] retuns the reference of 2 nd tag. Introduced by Internet Explorer

Example   Example Heading   This is a paragraph. It is only a paragraph.  Yet another paragraph.  This final paragraph has special emphasis. 

How many tags are there  document.all.length, returns the number of tag in the web page.

What are the name of tags   var no = document.all.length;  alert(no);  for(i=0;i<no;i++) document.write(" “ + document.all[i].tagName); 

Output Total number of tags: 16 HTML HEAD TITLE BODY H1 HR P EM EM P EM P EM EM HR SCRIPT

Final Note  We will learn about more JavaScript Object introduced by new browsers in next chapter.