EIW: Javascript DOM and Events1 JavaScript Document Object Model & Events.

Slides:



Advertisements
Similar presentations
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
Advertisements

JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
Chapter 6 © 2012 by Addison Wesley Longman, Inc Introduction Def: A dynamic HTML document is one whose tag attributes, tag contents, or element.
Introduction to JavaScript Module 1 Client Side JS Programming M-GO Sponsored By
The Web Warrior Guide to Web Design Technologies
Chapter 7 © 2001 by Addison Wesley Longman, Inc. 1 Chapter 7 Sebesta: Programming the World Wide Web.
20-Jun-15 JavaScript and HTML Simple Event Handling.
1 JavaScript Document Object Model & Events. 2 Document Object Model (DOM) JavaScript access to the elements of an HTML document. An object hierarchy.
Computer Science 103 Chapter 4 Advanced JavaScript.
Cos 381 Day 7. © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Agenda Assignment 2 Posted –Program a web-based Version of Soduku using JavaScript.
© De Montfort University, Document Object Model Howell Istance School of Computing De Montfort University.
UNIT 6 JAVASCRIPT EVENT HANDLERS &WEB BROWSERS DETECTION.
CP476 Internet Computing JavaScript and HTML1 1.JavaScript Execution Environment The JavaScript Window object represents the window in which the browser.
HTML Forms/Events (Instructor Lesson) The Event model HTML Forms Custom Events 1.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Event and Event Handling.
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.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
Event Handlers CS101 Introduction to Computing. Learning Goals Learn about event handlers Determine how events are useful in JavaScript Discover where.
Chapter 5 © 2003 by Addison-Wesley, Inc. 1 Chapter 5 JavaScript and HTML Documents.
Execution Environment for JavaScript " Java Script Window object represents the window in which the browser displays documents. " The Window object provides.
Chapter 5 JavaScript and HTML Documents. © 2006 Pearson Addison-Wesley. All rights reserved JavaScript Execution Environment - The JavaScript.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
UFCEWT-20-3 Advanced Topics in Web Development Lecture 4 : JavaScript, Browsers & the HTML DOM-API.
DHTML: Dynamic HTML Internet Technology1. What is DHTML? A collection of enhancements to HTML ► To create dynamic and interactive websites Combination.
Integrating JavaScript and HTML5 HTML5 & CSS 7 th Edition.
Creating an Animated Web Page
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.
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.
DOM Events. JS Events Outline About Events – Introduction – Registering events – Getting information from events Event order/phases – Preventing default.
Chapter 5 © 2005 by Addison Wesley Longman, Inc JavaScript Execution Environment - The JavaScript Window object represents the window in which the.
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
Event JavaScript's interaction with HTML is handled through events that occur when the user or browser manipulates a page. When the page loads, that is.
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 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
JavaScript Challenges Answers for challenges
Project 8: Reacting to Events Essentials for Design JavaScript Level One Michael Brooks.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
COS 125 DAY 20. Agenda Assignment 8 not corrected yet Assignment 9 posted  Due April 16 New course time line Discussion on Scripts 
7. JavaScript Events. 2 Motto: Do you think I can listen all day to such stuff? –Lewis Carroll.
H.Melikyan/4910/031 Programming the World Wide Web JavaScript Dr.Hayk Melikyan Departmen of Mathematics and CS JavaScript and HTML Documents.
Project Two Adding Scrolling Messages! Page 2.4 to 2.18 Today’s Agenda Look at scrolling message coding. Create a web page with a text box. Create a web.
Host Objects: Browsers and the DOM
Chapter 6 © 2014 by Pearson Education Introduction Def: A dynamic HTML document is one whose tag attributes, tag contents, or element style properties.
Document Object Model Nasrullah. DOM When a page is loaded,browser creates a Document Object Model of the Page.
JavaScript Document Object Model 1. Document Object Model (DOM) JavaScript access to the elements of an HTML document. An object hierarchy Provides access.
LESSON : EVENTS AND FORM VALIDATION -JAVASCRIPT. EVENTS CLICK.
This is our seminar JavaScript And DOM This is our seminar JavaScript And DOM.
Chapter 6 Murach's JavaScript and jQuery, C6© 2012, Mike Murach & Associates, Inc.Slide 1.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
Chapter 6 Dynamic Documents with JavaScript. © 2006 Pearson Addison-Wesley. All rights reserved Introduction Def: A dynamic HTML document is.
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 5 Host Objects: Browsers.
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Handling events from DOM objects.
5.1 JavaScript Execution Environment
JavaScript and HTML Simple Event Handling 11-May-18.
Week 3: Introduction to Javascript
Week 4: Introduction to Javascript
JavaScript and HTML Simple Event Handling 19-Sep-18.
JavaScript Introduction
DHTML Javascript Internet Technology.
DHTML Javascript Internet Technology.
JavaScript and Ajax (JavaScript Events)
Web Programming and Design
Web Programming and Design
6.1 Introduction 6.2 Positioning Elements
JavaScript and HTML Simple Event Handling 26-Aug-19.
JavaScript and HTML Simple Event Handling 4-Oct-19.
Presentation transcript:

EIW: Javascript DOM and Events1 JavaScript Document Object Model & Events

EIW: Javascript DOM and Events2 Document Object Model (DOM) JavaScript access to the elements of an HTML document. An object hierarchy Provides access to elements via: –ID ( ID attribute of HTML tags) –Order of elements in document –Order of element in collection of similar elements.

EIW: Javascript DOM and Events3 Warning! Various browsers support various version of the DOM. If you use latest version of Netscape or IE things will work as described here. Old browsers (or any other browsers) may not support the DOM described here…

EIW: Javascript DOM and Events4 First Example This is the best paragraph in the document. It is the best because it doesn't contain any words that have the letter 'e'. If you think it is impossible, load me and find out! b = document.getElementById("bestparagraph"); b.innerHTML="Hi world!"; Told you so dom1.html innerHTML is an attribute of an object that corresponds to an HTML tag. It’s value is the stuff between the start tag and the end tag. this will be the ID we use to locate the object that represents this paragraph

EIW: Javascript DOM and Events5 Using IDs If you assign an ID attribute to all your HTML tags, you can access the objects that correspond to those elements directly. The JavaScript document object supports a method that allows you to get at the object that represents an HTML tag with a specific ID. document.getElementById(“foo”); You must use unique names!

EIW: Javascript DOM and Events6 Important Note Generally you should not have JavaScript variables with the same name as an ID you use to identify an HTML element. –Some browsers automatically create an variable with the same name as an ID attribute. –Some browsers don't.

EIW: Javascript DOM and Events7 Collections DOM also supports collections of objects, –in Javascript these collections are represented as arrays of objects. Every HTML element has a childNodes collection. The document object has collections forms, images, and links in addition to childNodes.

EIW: Javascript DOM and Events8 document.all IE supports the document.all collection, which includes an object for every element (tag) in the document. –this is not supported by the W3C DOM standard and is no longer supported by Netscape/Mozilla. It's best to avoid using document.all, but you are likely to see it used in scripts…

EIW: Javascript DOM and Events9 Viewing the images collection var txt=""; for (var i=0; i<document.images.length;i++) { image = document.images[i]; name = image.getAttribute("src"); txt = txt + name +" "; } document.writeln("Here are the images found: \n"); document.write(txt); Add this to the bottom of any HTML document dom2.html

EIW: Javascript DOM and Events10 childNodes collections childNodes : just immediate descendants (so subelements are not in the collection). Each member of the collection has it's own childNodes collection! You can write a recursive function that can be used to display the structure of a document by looking through the childNodes collections. recursion.html

EIW: Javascript DOM and Events11 CSS properties Style properties can be accessed through the DOM. –and can be changed in response to user generated events. document.body.style.backgroundColor="blue"; for (i=0;i<document.childNodes.length;i++) { document.childNodes[i].style.fontFamily= "Courier"; }

EIW: Javascript DOM and Events12 Element CSS position properties You can also mess with CSS position properties Hi Joe document.getElementById(‘joe’).style.left = 200; dom4.html

EIW: Javascript DOM and Events13 DOM Issues How to access objects that correspond to elements of the document. –by ID, or in collections. How to know what the various object attributes are that change document element properties –need to look up the names in a DOM reference.

EIW: Javascript DOM and Events14 JavaScript Events It is possible to have the browser run JavaScript programs in response to certain events: –user events (moving mouse, clicking, keypress, copy/paste, etc). –document/window events (loading, unloading, resizing, scrolling, etc). –form submission –timed events

EIW: Javascript DOM and Events15 ONLOAD event The onLoad event is triggered when an element is loaded by the browser. You can put an onLoad handler in the BODY tag: A title Here is a paragraph … dom5.html

EIW: Javascript DOM and Events16 Another onLoad example function start() { window.setInterval("updateTime()",1000); } var seconds=0; function updateTime() { seconds++; document.getElementById("time").innerHTML = seconds; } Sample Document You have been here 0 seconds. dom6.html

EIW: Javascript DOM and Events17 window.setInterval(prog,delay) Schedules the JavaScript program prog to run at a time delay ms. in the future and at regular intervals after that. prog must be a string (that contains JavaScript code). You can stop the code from running with the help of window.clearInterval()

EIW: Javascript DOM and Events18 onMouseMove event The onMouseMove event is triggered whenever the mouse is moving. Can get at the x,y position using the event object. Here is the example that uses this event to display current mouse coordinates when over a specific element of the document: mousemove.html mousemove.html

EIW: Javascript DOM and Events19 event object Whenever an event causes JavaScript code to run, an object named event is created. You can find out about the event using this object. –the previous example used clientX and clientY.

EIW: Javascript DOM and Events20 event object attributes clientX, clientY : coordinates of the mouse. currentTarget : the object that caused the event (not supported by IE). type : the name of the event timeStamp eventobject.html

EIW: Javascript DOM and Events21 More events onMouseOver,onMouseOut –mouse moves over or leaves an element. onHelp –user asks for help (F1 or browser help button). onKeyDown, onKeyUp, onKeyPress – keyboard events onUnLoad –element (could be the document) is unloaded (closed).

EIW: Javascript DOM and Events22 Form Fields You can use the DOM to get the value of a form field, or to set the value of a form field. Assign an ID to each form field. –use the value attribute to access the current value.

EIW: Javascript DOM and Events23 Form Field Example Name: <INPUT TYPE=BUTTON VALUE="Check" onClick="validate();"> function validate() { fld = document.getElementById("nfield"); if (fld.value != "Dave") { alert("The name you typed is wrong"); fld.value="Dave"; } formexample.html