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.

Slides:



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

Computer and Communication Fundamental Basic web programming Lecture 8 Rina Zviel-Girshin.
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
The Web Warrior Guide to Web Design Technologies
Georgia Institute of Technology JavaScript part 2 Barb Ericson Georgia Institute of Technology May 2006.
20-Jun-15 JavaScript and HTML Simple Event Handling.
Computer Science 103 Chapter 4 Advanced JavaScript.
JavaScript 101 Lesson 5: Introduction to Events. Lesson Topics Event driven programming Events and event handlers The onClick event handler for hyperlinks.
Web Development & Design Foundations with XHTML Chapter 14 Key Concepts.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
CST JavaScript Validating Form Data with JavaScript.
1 Events Lect 8. 2 Event-driven Pages one popular feature of the Web is its interactive nature e.g., you click on buttons to make windows appear e.g.,
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.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Event and Event Handling.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Chapter 19: Adding JavaScript
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.
JavaScript Part 1.
Internet Mark-up Languages CO32036 Part Lecture: Elementary JavaScript.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
Adding User Interactivity – Lesson 51 Adding User Interactivity Lesson 5.
Section 17.1 Add an audio file using HTML Create a form using HTML Add text boxes using HTML Add radio buttons and check boxes using HTML Add a pull-down.
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.
Using Client-Side Scripts to Enhance Web Applications 1.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
JavaScript, Fourth Edition Chapter 5 Validating Form Data with JavaScript.
CO1552 Web Application Development HTML Forms, Events and an introduction to JavaScript.
JAVASCRIPT QUIZ BOWL By: Joseph Lindo. Quiz Bowl Mechanics  Group w/ 5 maximum members  Question is presented at the board  Prepare piece of papers.
Client-Side Scripting JavaScript.  produced by Netscape for use within HTML Web pages.  built into all the major modern browsers. properties  lightweight,
JavaScript - A Web Script Language Fred Durao
JavaScript - Basic Concepts Prepared and Presented by Hienvinh Nguyen, Afshin Tiraie.
Introduction to JavaScript CS101 Introduction to Computing.
1 JavaScript 2 JavaScript. 2 Rollovers Most web page developers first use JavaScript for rollovers A rollover is a change in the appearance of an element.
Scott Marino MSMIS Summer Session Web Site Design and Authoring Session 8 Scott Marino.
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.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Forms Collecting Data CSS Class 5. Forms Create a form Add text box Add labels Add check boxes and radio buttons Build a drop-down list Group drop-down.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
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.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (NON-Audio version) © Dr. David C. Gibbs WDMD.
COS 125 DAY 20. Agenda Assignment 8 not corrected yet Assignment 9 posted  Due April 16 New course time line Discussion on Scripts 
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 7 TH EDITION Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
7. JavaScript Events. 2 Motto: Do you think I can listen all day to such stuff? –Lewis Carroll.
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.
1 Lesson 6 Introducing JavaScript HTML and JavaScript BASICS, 4 th Edition.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
HTML Tutorial. What is HTML HTML is a markup language for describing web documents (web pages) HTML documents are described by HTML tags Each HTML tag.
Functions Wouldn’t it be nice to be able to bring up a new animal and paragraph without having to reload the page each time? We can! We can place the.
Copyright © Terry Felke-Morris Web Development & Design Foundations with HTML5 8 th Edition CHAPTER 14 KEY CONCEPTS 1 Copyright.
Chapter 5 Validating Form Data with JavaScript
JavaScript and HTML Simple Event Handling 11-May-18.
In this session, you will learn to:
Web Development & Design Foundations with HTML5
Web Development & Design Foundations with HTML5 7th Edition
JavaScript and HTML Simple Event Handling 19-Sep-18.
JavaScript Events.
JavaScript: Introduction to Scripting
JavaScript and Ajax (JavaScript Events)
JavaScript for Beginners
Web Programming and Design
JavaScript and HTML Simple Event Handling 26-Aug-19.
JavaScript and HTML Simple Event Handling 4-Oct-19.
Presentation transcript:

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

Add JavaScript to the Page JavaScript is a scripting language –allows the web programmer to write small programs that run inside the web browser – these programs allow the browser to perform complicated actions. Example: verification of a number typed into a web form

Examples of What JavaScript Does Open or pop up a new window ( alert ). Can control size, position and attributes of the window. –Are the menus and toolbars visible, for example Validate web form input values to make sure they'll be accepted before being submitted to the server. Example. Change images as the mouse cursor moves over them. Example.

Add JavaScript to the Page script JavaScript uses an object-based model Can identify and manipulate most of the elements on the page by seeing them as elements Almost every XHTML element placed in a Web page can be treated as an object in the script Frequently have to add an id attribute to an element in order for the script to differentiate it on a page

Add JavaScript to the Page script Its uses rely on the code being embedded into the page Do this by using the script element Takes one required attribute, type, which will always be set as text/javascript Anything between the opening and closing script tags will be interpreted by the browser’s Javascript engine

Add JavaScript to the Page Conventions JavaScript is case-sensitive Keywords and phrases all use lowercase letters or mixed case Variables can be created in any case (try to be consistent) References to elements from your Web page must match the case in which they exist in the code

Add JavaScript to the Page Conventions Each statement in JavaScript should end in a semicolon script element can appear in either the head or body –If in the head, the JS will execute when the page first loads –If in the body, the JS will execute when the browser reaches that portion of the page

Javascript Exercise

JavaScript Add Event Handlers JavaScript known as an event-driven language Nothing happens unless the user initiates the action –Clicking a button –Mousing over –Click hyperlink

JavaScript Add Event Handlers JavaScript events triggered by adding an event handler to an XHTML element. Most elements support: Onclick Ondblclick Onkeydown Onkeypres Onkeyup Onmousedown Onmousemove Onmouseout Onmouseover Onmouseup

JavaScript Add Event Handlers Form controls and the anchor element also support onblur and onfocus events Input, select, and textarea elements support onchange Input and textarea elements further select an onselect event check

JavaScript Add Event Handlers Scripts to be triggered by an event will need to be written as functions in the script Write functions by using the keyword function, followed by the function’s name and a set of parentheses that include the function’s arguments. function showalert() { alert (“Hello, world”); }

JavaScript Add Event Handlers Function will most likely be included in a script block in the head of the document Can be called by using its name as the value of the appropriate event handler: <button name=“triggerevent” Onclick=“showalert();”>Show dialog

JavaScript Add Event Handlers Documenting code: single-line comment

Validate a Form Did the User Enter Data Correctly? Examples: Is the data appropriate? XHTML has no concept of data typing to require numeric input Using JavaScript, you can write a function that verifies the input from user Looking for matches to what you need for the processing codeM

You Can Use JavaScript to Say This!

Validate a Form Did the User Enter Data Correctly? Any form you intend to validate should have name attribute Form exists in JavaScript as a document object that is a child of the document object Each form control is a child of the form

Validate a Form Did the User Enter Data Correctly? Dot.notation: document.signup.firstname JavaScript uses dot.notation to reference child objects Example: input field in a signup form can be called: document.signup.firstname

Validate a Form Did the User Enter Data Correctly? The value of document.signup.firstname can be accessed via document.signup.firstname.value Document will always be lowercase and it must match those given in the XHTML

Validate a Form Did the User Enter Data Correctly? To validate the controls, you will rely heavily on JavaScript if statements if syntax: provide logical test in parentheses following if The code to execute if the statement is true in curly braces Can use else statement to set code to execute if statement is false

Validate a Form Did the User Enter Data Correctly? == means equality != means unequality Test to see if a field was left empty by comparing its value to an empty string, or “”

Validate a Form Did the User Enter Data Correctly? Check boxes and radio buttons –Can contain multiple values, they exist within the form as an array –Need to loop over the values to test

Validate a Form Did the User Enter Data Correctly? Example:

Validate a Form Did the User Enter Data Correctly? Validation needs to be triggered by an event Use onsubmit

Open a New Window open The JavaScript function to generate a new window is simply open This function takes a series of arguments Only required one is URL to page that will be opened in the window Second optional argument lets you set name for the window

Open a New Window open Specify how the new window looks: Toolbars, location/address bar, status bar, menu bar, scroll bars, resizable window Set each of the above to the value of true Can also set width and height to size the window Top and left to position it relative to the top and left of user’s monitor

Hide and Show Elements visibility Change the value of the visibility element to hide or show content See “javascriptshowhide.html”

Swap Images onmouseover and onmouseout Rollover Example: javascriptswapimage.html Also: Joe MallerJoe Maller

Swap Images onmouseover and onmouseout Need two images Create an image, make a (subtle) change, save again Insert images into a Javascript rollover script When moused over, change takes place Images need to be same size

Swap Images onmouseover and onmouseout CSS: –make changes to text links: making them bigger, smaller, glow, change color, etc. –most common use is buttons, nav links

Swap Images onmouseover and onmouseout Place original image on page using img element Include an id Wrap in an anchor tag Set anchor’s href to a Javascript call using javascript:; Add onmouseover and onmouseout event calls

Swap Images onmouseover and onmouseout Javascript requirements: in the function onmouseover, get the element using document.getElementbyID, referencing the image’s id then set its src to the second image Onmouseout reverses this, setting src back to original

Swap Images onmouseover and onmouseout If there are many images: –Simplify code by passing a reference to the image as an argument in the function –Then use this reference as the value for get ElementById –Pass a second argument by with the desired source of the image Advantage is this technique has only a single pair of functions (regardless of image number)

Debug Javascript