JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.

Slides:



Advertisements
Similar presentations
Chapter 08: Adding Adding Interactivity Interactivity With With Behaviors Behaviors By Bill Bennett Associate Professor MSJC CIS MVC.
Advertisements

5.1 JavaScript Execution Environment
Computer and Communication Fundamental Basic web programming Lecture 8 Rina Zviel-Girshin.
Document Object Model (DOM) JavaScript manipulation of the DOM.
CS7026 jQuery Events. What are Events?  jQuery is tailor-made to respond to events in an HTML page.  Events are actions that can be detected by your.
JavaScript Forms Form Validation Cookies. What JavaScript can do  Control document appearance and content  Control the browser  Interact with user.
Georgia Institute of Technology JavaScript part 2 Barb Ericson Georgia Institute of Technology May 2006.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Cos 381 Day 9.
JavaScript Client Side scripting. Client-side Scripts Client-side scripts, which run on the user’s workstation can be used to: Validate user inputs entered.
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.
JS: DOM Form Form Object Form Object –The Form object represents an HTML form. –For each instance of a tag in an HTML document, a Form object is created.
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.
JavaScript - Document Object Model. Bhawna Mallick 2 Unit Plan What is DOM and its use? Hierarchy of DOM objects. Reflection of these objects in an HTML.
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.
Event Handlers CS101 Introduction to Computing. Learning Goals Learn about event handlers Determine how events are useful in JavaScript Discover where.
Chapter 19: Adding JavaScript
Copyright ©2005  Department of Computer & Information Science Introducing DHTML & DOM: JavaScript & Forms.
JavaScript II ECT 270 Robin Burke. Outline JavaScript review Processing Syntax Events and event handling Form validation.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 JavaScript and HTML Documents.
JavaScript & DOM Client-side scripting. JavaScript JavaScript is a tool to automate client side (which is implemented using HTML so far) JavaSript syntax.
DOM and JavaScript Aryo Pinandito.
Lecture 11 – DOM Scripting SFDV3011 – Advanced Web Development Reference: 1.
D2L Notes Be sure to submit your link in the dropbox provided on D2L You can just upload an empty text file if a file upload is required Do not use D2L.
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.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
CO1552 Web Application Development HTML Forms, Events and an introduction to JavaScript.
Intro to JavaScript Scripting language –ECMAScript compliant –Client side vs. server side Syntactic rules –White space –Statement end: ; optional –Comments:
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.
Jaana Holvikivi 1 Introduction to Javascript Jaana Holvikivi Metropolia.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
Making dynamic pages with javascript Lecture 1. Java script java versus javascript Javascript is a scripting language that will allow you to add real.
Event Handling. Objectives Using event handlers Simulating events Using event-related methods.
Project 8: Reacting to Events Essentials for Design JavaScript Level One Michael Brooks.
COS 125 DAY 20. Agenda Assignment 8 not corrected yet Assignment 9 posted  Due April 16 New course time line Discussion on Scripts 
Web Programming Java Script & jQuery Web Programming.
Dr. Ahmet Cengizhan Dirican BIL 374 Internet Technologies 5. JavaScript and HTML Documents.
5 th and 4 th ed: some from chapters 9, 12, 13 SY306 Web and Databases for Cyber Operations Slide Set #8: Dynamic HTML.
7. JavaScript Events. 2 Motto: Do you think I can listen all day to such stuff? –Lewis Carroll.
Document Object Model Nasrullah. DOM When a page is loaded,browser creates a Document Object Model of the Page.
JavaScript II ECT 270 Robin Burke. Outline Functions Events and event handling Form validation.
JavaScript Events.
JavaScript Event Handlers. Introduction An event handler executes a segment of a code based on certain events occurring within the application, such as.
LESSON : EVENTS AND FORM VALIDATION -JAVASCRIPT. EVENTS CLICK.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
Introduction to JavaScript Events Instructor: Sergey Goldman 1.
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Handling events from DOM objects.
THE DOM.
Chapter 5 Validating Form Data with JavaScript
JavaScript and HTML Simple Event Handling 11-May-18.
Web Development & Design Foundations with HTML5 7th Edition
JAVASCRIPTS AND HTML DOCUMENTS
JavaScript and HTML Simple Event Handling 19-Sep-18.
JavaScript Events.
DHTML Javascript Internet Technology.
JavaScript and HTML Documents
DHTML Javascript Internet Technology.
5.1 JavaScript Execution Environment
JavaScript and Ajax (JavaScript Events)
Barb Ericson Georgia Institute of Technology May 2006
Web Programming and Design
JavaScript and HTML Simple Event Handling 26-Aug-19.
JavaScript and HTML Simple Event Handling 4-Oct-19.
Presentation transcript:

JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001

Behavioral Layer Web pages have 3 layers… Structural/Content Layer (XHTML) –The meat and potatoes Presentational Layer (CSS) –How things look; garnishing the meat and potatoes on a pretty plate Behavioral Layer (JavaScript and DOM) –How websites behave; the meat can jump off the plate if you want it to.

Client-side Languages User-agent (web browser) requests a web page http request http response Web page (with JavaScript) is sent to PC JavaScript is executed on PC Can affect the Browser and the page itself

Client-side What kind of things can you do with JavaScript? Validating Form information, –i.e., making sure all the fields are complete before submitting data back to the server Modifying a web page based on Mouse Events. –Can turn a web page into a user interface with interactive buttons and controls

Server-side Languages User-agent (web browser) requests a web page http request Server detects PHP code in page, executes the code, and sends the output to the user http response Web page (with PHP Output) sent to PC User never sees the PHP, only the output Cannot affect the browser or client PC

JavaScript Java Functions definitions are embedded in the Function calls are placed in the function myfun() { do something; } myfun();

DOM Scripting Key Topics: Event Handling The Browser Object Document Object Model –the document structure

Event Handling JavaScript code can be initiated by browser events –HTML 4.0 supports lots of events. –onclick, onchange, onmousedown, onmousemove, etc.

Browser Events onblur – an element loses focus, i.e., click on a text box, but then you click on something else; the text box is blurred onchange – contents of an element is changed, i.e., changing the selection in a drop down menu onfocus – an element is clicked or selected onload – when the web page is initially loaded onsubmit – when a form’s submit button is clicked

More Browser Events onkeydown – immediately when a key is pressed down onkeypress – if the key is held down, i.e., not immediately released onkeyup – immediately when a key is released. –Sometimes, you want something to happen when the key goes down vs. goes up –Sometimes, you want to detect a long key press SHIFT, CTRL, or ALT

Even More Browser Events onmousedown – a mouse button is pressed down onmouseup – a mouse button is released onmousemove – a mouse is moved onmouseout – mouse is moved off an element (blur)* onmouseover – mouse is moved on an element (focus, hover)* * Used for hover effects.

Example Example Javascript Event Handler Here is some text with a sensitive patch in it

Example 2 function log(s){window.status=s} Example Javascript Event Handler Here is some text with a sensitive patch in it

Example 3 function log(s){window.status=s} Example Javascript Event Handler Here is some text with an insensitive patch in it

DOM Scripting First a summary: 1.JavaScript can be initiated by browser events. 2.JavaScript can access and manipulate the browser object. What’s Next JavaScript can access the document structure.

DOM Example This is a paragraph linking to Southampton. This is a paragraph linking to a. href=" p Element Nodes Attribute Node Text Nodes

DOM Script Example Main Menu Page Title Blaa blaa blaa.... Main Menu div id=“content” h1 Page Title div id=“menu” h1p Blaa blaa blaa. body

DOM Script Example Main Menu Page Title Blaa blaa blaa. var contentdiv = document.getElementById('content'); var pagetitle = contentdiv.getElementsByTagName('h1')[0]; pagetitle.setAttribute("style","color:red"); pagetitle.firstChild.nodeValue="The Red Page Title";

DOM Script Example var themenu = document.getElementById(‘menu’); var thebody = menu.parent; var thecontent = menu.nextSibling; var contentnodes = thecontent.childNodes; var theh1 = contentnodes[0]; var firstparagraph = contentnodes[1]; Main Menu div id=“content” h1 Page Title div id=“menu” h1p Blaa blaa blaa. body

DOM Scripting Functions Modifying Structure insertBefore() appendChild() replaceChild() removeChild() cloneNode() Creating Elements createElement() createTextNode() Modifying Attributes getAttribute() / setAttribute()

DOM and Forms Every form in a page is held in an array –document.forms[0] is the first form Every component (input, select or textarea element) is held in a subarray –document.forms[0].elements[0] is the first field either document.forms[0].elements[1] or document.forms["personal"]. elements["address"] or document.personal.address

DOM and Forms Every component of the form has a value –document.personal.address.value The value can be used in expressions or stored in an assignment statement Specific components have specific methods or properties –a menu (ie a select) has property ‘selectedIndex’ –a checkbox has property ‘checked’ An onsubmit event handler can check its form’s components and halt the submission by returning false

DOM and Forms function validate(){ if(document.personal.name.value.length==0){ alert("Missing name"); return false; } return true; }