Oct 25 1 The Information School of the University of Washington fit100-13-control © 2006 University of Washington Control Flow INFO/CSE 100, Fall 2006.

Slides:



Advertisements
Similar presentations
23-Aug-14 HTML/XHTML Forms. 2 What are forms? is just another kind of XHTML/HTML tag Forms are used to create (rather primitive) GUIs on Web pages Usually.
Advertisements

24-Aug-14 HTML Forms. 2 What are forms? is just another kind of HTML tag HTML forms are used to create (rather primitive) GUIs on Web pages Usually the.
Java Script Session1 INTRODUCTION.
The Web Warrior Guide to Web Design Technologies
JavaScript Forms Form Validation Cookies. What JavaScript can do  Control document appearance and content  Control the browser  Interact with user.
1 The Information School of the University of Washington Nov 1fit dom © 2006 University of Washington Document Object Model (DOM) INFO/CSE 100, Fall.
Chapter 7 © 2001 by Addison Wesley Longman, Inc. 1 Chapter 7 Sebesta: Programming the World Wide Web.
1 The Information School of the University of Washington Oct 30fit review Programming Review INFO/CSE 100, Fall 2006 Fluency in Information Technology.
Forms Review. 2 Using Forms tag  Contains the form elements on a web page  Container tag tag  Configures a variety of form elements including text.
The Information School of the University of Washington Oct 23 fit functions 1 Functions / If Else INFO/CSE 100, Fall 2006 Fluency in Information.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
The Information School of the University of Washington Dec 8fit review1 Final Exam Review INFO/CSE 100, Fall 2006 Fluency in Information Technology.
29 November JavaScript: Arrays and Iterations Functions.
Computer Science 103 Chapter 4 Advanced JavaScript.
JavaScript Lesson 1 TBE 540. Prerequisites  Before beginning this lesson, the learner must be able to… Create a basic web page using a text editor and/or.
Tutorial 6 Forms Section A - Working with Forms in JavaScript.
XHTML Forms for Data Collection and Submission Chapter 5.
Forms, Validation Week 7 INFM 603. Announcements Try placing today’s example in htdocs (XAMPP). This will allow you to execute examples that rely on PHP.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
CST JavaScript Validating Form Data with JavaScript.
JavaScript Form Validation
CNIT 133 Interactive Web Pags – JavaScript and AJAX Event and Event Handling.
Database-Driven Web Sites, Second Edition1 Chapter 8 Processing ASP.NET Web Forms and Working With Server Controls.
Server vs Client-side validation. JavaScript JavaScript is an object-based language. JavaScript is based on manipulating objects by modifying an object’s.
1 Forms A form is the usual way that information is gotten from a browser to a server –HTML has tags to create a collection of objects that implement this.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Event-driven Programming
1 Week Three: Using Scripts and Forms in HTML (Week Three) By Dr Fadi Safieddine.
Event Handlers CS101 Introduction to Computing. Learning Goals Learn about event handlers Determine how events are useful in JavaScript Discover where.
The Bean Counter: A JavaScript Program
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
PHP meets MySQL.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Client Side Programming with JavaScript Why use client side programming? Web sides built on CGI programs can rapidly become overly complicated to maintain,
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.
HTML Forms.
Creating Web Page Forms. Introducing Web Forms Web forms collect information from users Web forms include different control elements including: –Input.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
HTML Form Widgets. Review: HTML Forms HTML forms are used to create web pages that accept user input Forms allow the user to communicate information back.
JavaScript - Basic Concepts Prepared and Presented by Hienvinh Nguyen, Afshin Tiraie.
44238: Dynamic Web-site Development Client Side Programming Ian Perry Room:C48 Extension:7287
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
JavaScript, Fourth Edition
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
HTML Forms.
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.
JavaScript Events Java 4 Understanding Events Events add interactivity between the web page and the user You can think of an event as a trigger that.
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
HTML Structure II (Form) WEEK 2.2. Contents Table Form.
Client-side (JavaScript) Validation. Associating a function with a click event – Part 1 Use the input tag’s onclick attribute to associate a function.
XHTML Forms.
Document Object Model (DOM)
Barb Ericson Georgia Institute of Technology May 2006
Document Object Model (DOM)
Document Object Model (DOM)
Web Programming– UFCFB Lecture 17
INFO/CSE 100, Spring 2005 Fluency in Information Technology
Document Object Model (DOM)
INFO/CSE 100, Spring 2006 Fluency in Information Technology
FORM OBJECT When creating an interactive web site for the Internet it is necessary to capture user input and process this input. Based on the result of.
Document Object Model (DOM)
INFO/CSE 100, Spring 2006 Fluency in Information Technology
INFO/CSE 100, Spring 2005 Fluency in Information Technology
INFO/CSE 100, Spring 2005 Fluency in Information Technology
Programming Basics Review
Presentation transcript:

Oct 25 1 The Information School of the University of Washington fit control © 2006 University of Washington Control Flow INFO/CSE 100, Fall 2006 Fluency in Information Technology

Oct 25 2 The Information School of the University of Washington fit control © 2006 University of Washington Readings and References Reading »Fluency with Information Technology Chapter 21, Iteration Principles

Oct 25 3 The Information School of the University of Washington fit control © 2006 University of Washington Comments on Debugging Debugging JavaScript can be hard »The browsers all implement things a little differently, particularly old browsers upgrade if you are using something old!

Oct 25 4 The Information School of the University of Washington fit control © 2006 University of Washington Use an editor that helps you The jEdit editor helps you by doing syntax highlighting.

Oct 25 5 The Information School of the University of Washington fit control © 2006 University of Washington Display results using alert(...) Use the alert("This is a message") function

Oct 25 6 The Information School of the University of Washington fit control © 2006 University of Washington Display results using writeln(...) document.writeln("someVariable: "+someVariable);

Oct 25 7 The Information School of the University of Washington fit control © 2006 University of Washington Use a browser that helps you All browsers try to be forgiving of errors, which means that they generally don't produce a lot of error messages »use a browser that helps you debug like Mozilla

Oct 25 8 The Information School of the University of Washington fit control © 2006 University of Washington enable Mozilla JavaScript Console

The Mozilla JavaScript console helps you by showing good error messages.

Oct The Information School of the University of Washington fit control © 2006 University of Washington Graphical User Interfaces (GUIs) We can also use JavaScript to create Graphical User Interfaces.

Oct The Information School of the University of Washington fit control © 2006 University of Washington GUIs A Graphical User Interface provides an intuitive way to control a program instead of having to memorize commands text fields with labels to request user entry text fields with labels to display results buttons to command action radio buttons and checkboxes to set conditions

Oct The Information School of the University of Washington fit control © 2006 University of Washington A simple example This GUI has several simple controls. Two buttons to control the results One text field to display the results One pair of radio buttons to control the display One button to reinitialize

Oct The Information School of the University of Washington fit control © 2006 University of Washington A simple example <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " Simple Sample GUI javascript function code HTML form layout and specification

Oct The Information School of the University of Washington fit control © 2006 University of Washington Layout of the GUI The layout of the page is controlled with HTML in the body of the page The layout and controls are provided using new tags » »<button type="button"... »<input type="text" … »<input type="radio" … »<button type="reset" … HTML form layout and specification

Oct The Information School of the University of Washington fit control © 2006 University of Washington HTML forms provide a way for the user to enter data into a web page »A form can contain several different types of entry, control, and display elements »The data in a form can be passed back to the web server, or it can be processed locally on the client All of our forms will processed locally A form is defined with the... tag »The form has various attributes like id, so we can refer to it and its elements later »the form contains various elements like and

Oct The Information School of the University of Washington fit control © 2006 University of Washington <button type="button" onclick="setResults('good results')">Good Results <button type="button" onclick="setResults('bad results')">Bad Results a can have one of three types »type “button” is used locally »type “submit” sends data back to the server »type “reset” re-initializes the form the value of the “onclick” attribute is some JavaScript code, in this case a call to the function setResults(string)

Oct The Information School of the University of Washington fit control © 2006 University of Washington Result: <input type="radio" name="case" id="radioLC" checked onclick="setResults(document.getElementById('resultField').value)">Lowercase <input type="radio" name="case" id="radioUC" onclick="setResults(document.getElementById('resultField').value)">Uppercase Reset an with type="text" is used for user input and program output value="nada" sets the initial (and reset) value readonly means that the user cannot set the value, only the script can set the value id="resultField" gives us a way to identify this particular control in our JavaScript

Oct The Information School of the University of Washington fit control © 2006 University of Washington a with type="reset" resets all the other controls in the same form to their original values Result: <input type="radio" name="case" id="radioLC" checked onclick="setResults(document.getElementById('resultField').value)">Lowercase <input type="radio" name="case" id="radioUC" onclick="setResults(document.getElementById('resultField').value)">Uppercase Reset

Oct The Information School of the University of Washington fit control © 2006 University of Washington an with type="radio" allows the user to select one of several choices name="case" identifies all the buttons in the same group (only one will be selected at a time) onclick attribute gives the JavaScript to execute when the user clicks this button id="radioLC" gives us a way to identify this particular control in our JavaScript Result: <input type="radio" name="case" id="radioLC" checked onclick="setResults(document.getElementById('resultField').value)">Lowercase <input type="radio" name="case" id="radioUC" onclick="setResults(document.getElementById('resultField').value)">Uppercase Reset

Oct The Information School of the University of Washington fit control © 2006 University of Washington Events Cause Processing After drawing a page, the browser sits idle waiting for something to happen … when we give input, we cause events Processing events is the task of a block of code called an event handler »The code to execute is identified in the tag using the appropriate attribute »There are many event types onClick, onChange, onMouseOver...

Oct The Information School of the University of Washington fit control © 2006 University of Washington request processing of an event the onclick attribute defines some JavaScript to call when the button is clicked in this case, the code is a call to the setResults(string) function defined in the page the appropriate string value is supplied to the setResults(string) function and then the function executes <button type="button" onclick="setResults('good results')">Good Results <button type="button" onclick="setResults('bad results')">Bad Results

Oct The Information School of the University of Washington fit control © 2006 University of Washington function setResults(resultString) { var tempString = resultString; if (document.getElementById("radioLC").checked) { tempString = tempString.toLowerCase(); } else if (document.getElementById("radioUC").checked) { tempString = tempString.toUpperCase(); } document.getElementById("resultField").value = tempString; } process a button’s onclick event the setResults(string) function is called by several event processors in every case, it takes the string that it is given, decides if upper or lower case is desired, and sets the resultField accordingly

Oct The Information School of the University of Washington fit control © 2006 University of Washington setResults(resultString) parameter variable, local variable, if/else statement, field reference, call to toLowerCase() function function setResults(resultString) { var tempString = resultString; if (document.getElementById("radioLC").checked) { tempString = tempString.toLowerCase(); } else if (document.getElementById("radioUC").checked) { tempString = tempString.toUpperCase(); } document.getElementById("resultField").value = tempString; }

Oct The Information School of the University of Washington fit control © 2006 University of Washington if statement in Simple Sample GUI the setResults(string) function is called by several event processors in every case, it takes the string that it is given, decides if upper or lower case is desired, and sets the resultField accordingly function setResults(resultString) { var tempString = resultString; if (document.getElementById("radioLC").checked) { tempString = tempString.toLowerCase(); } else if (document.getElementById("radioUC").checked) { tempString = tempString.toUpperCase(); } document.getElementById("resultField").value = tempString; }

Oct The Information School of the University of Washington fit control © 2006 University of Washington A Fancier Example of a GUI program a single tall latte, what a great way to start the morning

Oct The Information School of the University of Washington fit control © 2006 University of Washington Nested if/else Statements if (temp < 32) { if (sky == "cloudy) { alert("Snow is forecast!"); } if (temp < 32 && sky == "cloudy") { alert("Snow is forecast!"); }

Oct The Information School of the University of Washington fit control © 2006 University of Washington Iteration Iteration or looping is a way to execute a block of program statements more than once we will use the for statement to create loops »The for loop is generally controlled by counting »There is an index variable that you increment or decrement each time through the loop »When the index reaches some limit condition, then the looping is done and we continue on in the code

Oct The Information School of the University of Washington fit control © 2006 University of Washington Why do we want loops in our code? Do something for a given number of times or for every object in a collection of objects »for every radio button in a form, see if it is checked »for every month of the year, charge $100 against the balance »calculate the sum of all the numbers in a list »etc. Many loops are counting loops »they do something a certain number of times

Oct The Information School of the University of Washington fit control © 2006 University of Washington The for loop A counting loop is usually implemented with for for (var i=0; i < count; i++) { document.writeln(" index value is : "+i); } initialize check for limit update loop control index shorthand for i=i+1 one or more statements in the loop body var count = 10;

for example

Oct The Information School of the University of Washington fit control © 2006 University of Washington i++ is a shortcut for (i=0; i < count; i++) at the end of every pass through the for loop body, do the following: »get the value of i »increment i »store the incremented value Used as it is here, this is the same as writing »i = i + 1

Oct The Information School of the University of Washington fit control © 2006 University of Washington body of loop may not execute at all Notice that depending on the values of the control variables, it is quite possible that the body of the loop will not execute at all var itemCount = 0;... for (var i=0; i < itemCount; i++) { document.writeln("..processing item "+i); } check for limit condition itemCount is 0 when we get here, so i<itemCount is immediately false and the loop body is skipped completely

Oct The Information School of the University of Washington fit control © 2006 University of Washington loop body skip

Oct The Information School of the University of Washington fit control © 2006 University of Washington “Off By 1” Error The most common error when working with iterations is to miscount by 1 »Everyone makes this mistake »A common place where the “off by 1” error matters is in how many times a loop loops »One advantage of a simple loop control statement is that it's easier to tell how many loops there will be Number of iterations for ( var i=0; i<n; i++) { }

Oct The Information School of the University of Washington fit control © 2006 University of Washington Avoid Infinite Loops var count = 10; for ( var i=0; i<count; j++) { document.write("All work and no play, makes Jack a dull boy."); }

Oct The Information School of the University of Washington fit control © 2006 University of Washington Homework / Projects Homework 3 - Due Monday Oct start of class »Programming questions. Verify your answers! Project 2a - Due Monday Nov 10:00 PM »Hangman Game