JavaScript DOM.

Slides:



Advertisements
Similar presentations
Tutorial 5 Windows and Frames Section A - Working with Windows.
Advertisements

Introduction to HTML & CSS
JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
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.
Dawn Pedersen Art Institute. What is Spry? Spry is Dreamweaver’s version of JavaScript libraries. Spry effects alter the look of a page element—or of.
The Web Warrior Guide to Web Design Technologies
Hypertext Markup Language. Platform: - Independent  This means it can be interpreted on any computer regardless of the hardware or operating system.
Web-based Application Development Lecture 9 February 7, 2006 Anita Raja.
1 Chapter 12 Working With Access 2000 on the Internet.
Tutorial 16 Working with Dynamic Content and Styles.
Chapter 16 Dynamic HTML and Animation The Web Warrior Guide to Web Design Technologies.
Javascript Document Object Model. How to use JavaScript  JavaScript can be embedded into your html pages in a couple of ways  in elements in both and.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
JavaScript Part 1. Slide 2 Lecture Overview JavaScript background The purpose of JavaScript JavaScript syntax.
Introduction To Form Builder
Tutorial 13 Working with Objects and Styles. XP Objectives Learn about objects and the document object model Reference documents objects by ID, name,
Create a Web Site with Frames
© De Montfort University, Document Object Model Howell Istance School of Computing De Montfort University.
1 Agenda Overview Review Roles Lists Libraries Columns.
CST JavaScript Validating Form Data with JavaScript.
INTRODUCTION TO CLIENT-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes.
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
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Chapter 6: Forms JavaScript - Introductory. Previewing the Product Registration Form.
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.
HTML, XHTML, and CSS Chapter 12 Creating and Using XML Documents.
Unobtrusive JavaScript
Execution Environment for JavaScript " Java Script Window object represents the window in which the browser displays documents. " The Window object provides.
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.
JavaScript Introduction
CSS Class 7 Add JavaScript to your page Add event handlers Validate a form Open a new window Hide and show elements Swap images Debug JavaScript.
CITS1231 Web Technologies JavaScript and Document Object Model.
ASP.NET Controls. Slide 2 Lecture Overview Identify the types of controls supported by ASP.NET and the differences between them.
CIS 205—Web Design & Development Dreamweaver Chapter 1.
JavaScript, Fourth Edition
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
An Introduction to JavaScript Summarized from Chapter 6 of “Web Programming: Building Internet Applications”, 3 rd Edition.
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.
CSCE 102 – Chapter 6 (Web Design and Layout) CSCE General Applications Programming Benito Mendoza Benito Mendoza 1 By Benito Mendoza.
Tutorial 5 Windows and Frames Section B - Working with Frames and Other Objects Go to Other Objects.
Chapter 5: Windows and Frames
XP Tutorial 6 New Perspectives on JavaScript, Comprehensive1 Working with Windows and Frames Enhancing a Web Site with Interactive Windows.
Introduction to Programming the WWW I CMSC Summer 2003 Lecture 7.
JavaScript Introduction 1. What is Java Script ? JavaScript is a client-side scripting language. A scripting language is a lightweight programming language.
1 Introduction  Extensible Markup Language (XML) –Uses tags to describe the structure of a document –Simplifies the process of sharing information –Extensible.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
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.
Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling – Inline – Scripting –
JavaScript Introduction. Slide 2 Lecture Overview JavaScript background The purpose of JavaScript A first JavaScript example Introduction to getElementById.
DOM (Document Object Model) - Parsing and Reading HTML and XML -
Topic 5 Windows and Frames. 2 Goals and Objectives Goals Goals Understand JavaScript window object, how popup windows work, find the browser that a client.
Project 5: Using Pop-Up Windows Essentials for Design JavaScript Level One Michael Brooks.
Understanding JavaScript and Coding Essentials Lesson 8.
HTML DOM Basharat Mahmood, Department of Computer Science, CIIT, Islamabad, Pakistan 1.
Chapter 10 Dynamic HTML (DHTML) JavaScript, Third Edition.
XHTML Forms.
THE DOM.
DHTML.
Chapter 5 Validating Form Data with JavaScript
Using DHTML to Enhance Web Pages
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
JavaScript Introduction
Working with Dynamic Content and Styles
Drupal user guide Evashni Jansen Web Office.
Presentation transcript:

JavaScript DOM

Lecture Overview The Basics of the Document Object Model (DOM) DOM structure Navigating the DOM Manipulating elements

The Document Object Model There really is not that much to the JavaScript language itself It’s just a subset of Java JavaScript uses the Document Object Model (DOM) objects to get at the browser, its documents, and its windows This is where the real power comes in Other “things” can use the DOM too (.NET for example)

What is the DOM (1)? The HTML DOM is a tree of objects It permits access to the contents, structure, and style of an HTML 5 document An XML document too The DOM can communicate with multiple browser instances It’s formally defined by the W3C "The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows programs and scripts to dynamically access and update the content, structure, and style of a document."

What is the Dom (2)?

What is the DOM (3)? Use the DOM to hide and make elements visible change CSS styles dynamically create new document elements move document elements and remove them too

The DOM Hierarchy

DOM Programming Model It’s an object model and a programming interface HTML elements are considered objects They have properties to store data Some properties are read / write Some properties are read only innerHTML for example They have methods that perform actions getElementById for example They respond to events

We will Talk About document (the currently loaded document in the browser) navigator (the browser itself) window (a window in the browser)

The document Object The document object represents your running HTML document as it is seen by the browser We can find elements and set their properties change elements add and delete elements

The document Object (Finding Elements) Method Description document.getElementById() Find an element by element id document.getElementsByTagName() Find elements by tag name document.getElementsByClassName() Find elements by class name

The document Object (Changing Element Content) Method Description element.innerHTML= Change the inner HTML of an element element.attribute= Change the attribute of an HTML element element.setAttribute(attribute,value) element.style.property= Change the style of an HTML element

The document Object (Adding / Deleting Elements) Method Description document.createElement() Create an HTML element document.removeChild() Remove an HTML element document.insertBefore() Insert a new element document.appendChild() Add an HTML element document.replaceChild() Replace an HTML element document.write(text) Write into the HTML output stream See example in IS360JavaScriptDomExample.html

The document Object (A Canonical List) Refer to W3Schools http://www.w3schools.com/js/js_htmldom_document.asp

Referencing Elements by ID Remember each HTML element has an ID attribute This attribute is special – it’s the unique identifier for the node It’s value should begin with a letter for compatibility with all browsers Use the getElementById method to get a reference to the node The method applies to the document object

Referencing Elements by ID (Example) Get and change the text in the paragraph named First <script type="text/javascript"> function ShowTree() { x=document.getElementById("First"); x.innerHTML = "Changed"; } </script> The paragraph declaration <p id="First">Hello world!</p>

Getting Elements by Tag Name getElementsByTagName gets a list (array) of elements having a particular tag name The length property of the array tells us how many item are in the array Each element can be referenced via an array subscript

Getting Elements by Tag Name (Example) Collapse (hide) all paragraph elements

Getting Elements by Query Selector The querySelectorAll() method selects objects (elements) based on a CSS query selector These are the same query selectors that you have used before

Getting Elements by Query Selector Collapse all <p> tags in a <section>

Getting Elements by Class Name You can get all elements that belong to a particular class by calling getElementsByClassName The method accepts one argument – the class name The method returns an array of elements

Creating new Elements First create an element with createElement() Optionally put text in the element with createTextNode() Append the text to the above element Insert the element that you created in the DOM where you want it

Creating New Elements (Example)

Determining the Browser Use the navigator object to get info about the browser appVersion gets the major version Netscape Microsoft Internet Explorer appMinorVersion gets the minor version Supported by IE only appName gets the name of the browser

Determining the Browser Location geolocation gets coordinates for browsers that support it Getting the location is a multi-step process Call navigator.geolocation.getCurrentPosition() to get the current position Create a callback function to process the returned coordinates https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition

Determining the Browser Location

The window object The window object provides a reference to the open browser window Through the window object, you can Reference the elements on the page through the DOM Create new windows and destroy them

The window Object (Introduction) It’s the root of the IE object hierarchy It refers to the IE Browser windows The document property contains a reference to the open document More about the document object in a moment window.open() creates a new browser window

window.open (Semantics) window – refers to the browser window We can also use the keyword self window.open (url, name, features, replace) url contains URI or file name Second argument contains the name of the window Features allows browser window configuration It’s a comma-separated list of key=value pairs Replace, if false, creates a new history entry. If true, the current history entry is replaced

The window Object (Attributes 1) fullscreen - defines whether window fills the desktop toolbar – enable or disable the toolbar menubar – enable or disable the menu bar resizable – allow or disallow window resizing

The window Object (Attributes 2) alwaysRaised – browser floats on top of other windows regardless of whether it is active height and width define the window size scrollbars defines whether scroll bars appear when necessary Unspecified attributes will have false values

The window Object (Attributes – Example) Create a blank Web page without a toolbar or a menu bar Note attributes appear as a comma separated list 1 and yes are equivalent 0 and no are equivalent newWindow = window.open("","foo", "toolbar=no,menubar=no")

The window Object (Best practices) Do not use to create those dreaded banner ads Do not use to trap the user by handling onClose and displaying the window again Do not hide the title bar

The window Object (Example) Display a very annoying window that’s hard to get rid of window1 = window.open("","Annoy", "height=300,width=300,titlebar=no") window1.document.write("Annoying") See JavaScriptWindowMaker.htm

The window Object (Members 2) Display a prompt with a text entry field window.prompt(“message”, “Default”) Display a confirmation dialog if (window.confirm("Exit")) { window.close() } Display a message window.alert("Error")

The window Object (History 1) history – used for history navigation window.history.back() window.history.forward() Go back to pages and forward 2 pages window.history.go(-2) window.history.go(2)

The window Object (History 2) The history object also support the following properties: current – the URL of the current document length – the number of URLs in the history list next – the next URL in the history list previous – the previous URL in the history list

The window Object (Status bar) There are two properties to manage the status bar defaultStatus – this message always appears status – this message appears only temporarily such as when the mouse hovers over a button or link Display a status bar message window.status("appears in the status bar")