CSC 551: Web Programming Fall 2001 HTML forms & JavaScript events  HTML forms & attributes  button, text box, text area  selection list, radio button,

Slides:



Advertisements
Similar presentations
Tutorial 6 Creating a Web Form
Advertisements

1 CSC 551: Web Programming Spring 2004 client-side programming with JavaScript  scripts vs. programs  JavaScript vs. JScript vs. VBScript  common tasks.
JavaScript Forms Form Validation Cookies. What JavaScript can do  Control document appearance and content  Control the browser  Interact with user.
Forms cs105. More Interactive web-pages So far what we have seen was to present the information. (Ex: tables. Lists,….). But it is not enough. We want.
Chapter 7 © 2001 by Addison Wesley Longman, Inc. 1 Chapter 7 Sebesta: Programming the World Wide Web.
JavaScript Forms Form Validation Cookies CGI Programs.
Creating Web Page Forms. Objectives Describe how Web forms can interact with a server-based program Insert a form into a Web page Create and format a.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 7 Event-Driven.
COMP 519: Web Programming Autumn 2014 Event-driven programs and HTML form elements  event-driven programs  onload, onunload  HTML forms & attributes.
CSC4370/6370: Web Programming Event-driven programs and HTML form elements  event-driven programs  onload, onunload  HTML forms & attributes  button,
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.,
Client-Side programming with JavaScript 3
Javascript and the Web Whys and Hows of Javascript.
XP Tutorial 6New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Creating Web Page Forms Designing a Product Registration Form Tutorial.
Forms and Form Controls Chapter What is a Form?
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.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
1 SEN 910 CSS/HTML Programming Final Exam Review.
Chapter 6: Forms JavaScript - Introductory. Previewing the Product Registration Form.
Event-driven Programming
Event Handlers CS101 Introduction to Computing. Learning Goals Learn about event handlers Determine how events are useful in JavaScript Discover where.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
Copyright ©2005  Department of Computer & Information Science Introducing DHTML & DOM: JavaScript & Forms.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
JavaScript Part 1.
10 Adding Interactivity to a Web Site Section 10.1 Define scripting Summarize interactivity design guidelines Identify scripting languages Compare common.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
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.
Using Client-Side Scripts to Enhance Web Applications 1.
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.
1 Final Exam Review Web Programming with HTML and Java.
HTML Forms.
ITCS373: Internet Technology Lecture 5: More HTML.
1 Website Development HTML Forms and JavaScript Event-driven programs and HTML form elements  event-driven programs  ONLOAD, ONUNLOAD  HTML forms &
Client side scripting - javascript Pertemuan 4 Matakuliah: Web Programming Tahun: 2009.
Tutorial 6 Working with Web Forms. XP Objectives Explore how Web forms interact with Web servers Create form elements Create field sets and legends Create.
Tutorial 6 Working with Web Forms. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Explore how Web forms interact with.
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
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.
1 A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 4 JavaScript and.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
Client-Side programming with JavaScript 2. Event-driven programs and HTML form elements event-driven programs  onload, onunload  HTML forms & attributes.
Introduction To JavaScript. Putting it Together (page 11) All javascript must go in-between the script tags. All javascript must go in-between the script.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (NON-Audio version) © Dr. David C. Gibbs WDMD.
Tutorial 6 Working with Web Forms. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Explore how Web forms interact with.
HTML FORMS The TEXT Object Presented By: Ankit Gupta.
SEN 910 CSS/HTML Programming HTML Forms and JavaScript Event-driven programs and HTML form elements  event-driven programs  ONLOAD, ONUNLOAD  HTML forms.
1 CSC 551: Web Programming Spring 2004 Event-driven programs and HTML form elements  event-driven programs  ONLOAD, ONUNLOAD  HTML forms & attributes.
Event Handlers Events are asynchronous. When an event occurs, JavaScript can execute code in response to the user’s action. This response to the user-initiated.
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.
CSC 121 Computers and Scientific Thinking Fall Event-Driven Programming.
CGS 3066: Web Programming and Design Spring 2016 Introduction to JavaScript.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
Web Programming Java Script-Introduction. What is Javascript? JavaScript is a scripting language using for the Web. JavaScript is a programming language.
HTML Structure II (Form) WEEK 2.2. Contents Table Form.
XP Tutorial 6New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Creating Web Page Forms Designing a Product Registration Form Tutorial 6.
XHTML Forms.
Section 10.1 Define scripting
Chapter 5 Validating Form Data with JavaScript
Event-Driven Programming
In this session, you will learn to:
How to Write Web Forms By Mimi Opkins.
Section 17.1 Section 17.2 Add an audio file using HTML
Event-driven programs and HTML form elements
Chapter 6 Event-Driven Pages
Chapter 7 Event-Driven Pages
Presentation transcript:

CSC 551: Web Programming Fall 2001 HTML forms & JavaScript events  HTML forms & attributes  button, text box, text area  selection list, radio button, check box, password, hidden, …  JavaScript form events  properties: name, type, value, …  methods: blur(), focus(), click(), …  event handlers: onBlur(), onFocus(), onChange(), onClick(), …  advanced features & techniques  windows & frames, timeouts, cookies

HTML forms an HTML form is a collection of elements for handling input, output, and events in a page … form elements include: for input: button, selection list, radio button, check box, password, … for input/output: text box, text area, … we will revisit forms when we consider CGI programming  a form groups together elements, whose contents are submitted as one

Button element Fun with Buttons <input type="button" value="Click Me" onClick="alert('Thanks, I needed that.');"> view page in browser the simplest form element is a button  analogous to a real-world button, can click to trigger events attributes include: VALUE : specifies label that appears on the button ONCLICK : specifies code to be executed when clicked

Buttons & JavaScript Fun with Buttons <script language="JavaScript" src=" <input type="button" value="Click for Lucky Number" onClick="num = randomInt(1, 100); alert('The lucky number for the day is ' + num);"> view page in browser the ONCLICK event-handler can specify any JavaScript code  can be a sequence of statements inside quotes, can call functions, …

Buttons & functions Fun with Buttons function Greeting() // Results: displays a time-sensitive greeting { var now = new Date(); if (now.getHours() < 12) { alert("Good morning"); } else if (now.getHours() < 18) { alert("Good afternoon"); } else { alert("Good evening"); } <input type="button" value="Click for Greeting" onClick="Greeting();"> view page in browser for complex tasks, should define function(s) and have the ONCLICK event trigger a function call

Buttons & windows alert boxes are fine for displaying short, infrequent messages  not well-suited for displaying longer, formatted text  not integrated into the page, requires the user to explicitly close the box QUESTION: could we instead use document.write ? NO -- would overwrite the current page, including form elements but could open a new browser window and write there var OutputWindow = window.open("", "WinName");// open window and assign // a name to that object // (first arg is an HREF) OutputWindow.document.open();// open that window for // writing OutputWindow.document.write("WHATEVER");// write text to that // window as before OutputWindow.document.close();// close the window

Window example Fun with Buttons function Help() // Results: displays a help message in a separate window { var OutputWindow = window.open("", "OutWin"); OutputWindow.document.open(); OutputWindow.document.write("This might be a context-" + "sensitive help message, depending on the " + "application and state of the page."); OutputWindow.document.close(); } <input type="button" value="Click for Help" onClick="Help();"> view page in browser

Window example refined Fun with Buttons function Help() // Results: displays a help message in a separate window { var OutputWindow = window.open("", "OutWin", "status=0,menubar=0,height=200,width=200"); OutputWindow.document.open(); OutputWindow.document.write("This might be a context-" + "sensitive help message, depending on the " + "application and state of the page."); OutputWindow.document.close(); } <input type="button" value="Click for Help" onClick="Help();"> view page in browser can have a 3 rd argument to window.open specifies window properties (e.g., size)

Text boxes a text box allows for user input  unlike prompt, user input persists on the page & can be edited attributes include: NAME : name by which its contents can be referred SIZE : width of the box (number of characters) VALUE : initial contents of the box Fun with Text Boxes Enter your name here: <input type="button" name="clicker" value="Click Me" onClick="alert('Thanks, ' + document.BoxForm.userName.value + ', I needed that.');"> view page in browser

Read/write text boxes can access text box contents as document.FormName.BoxName.value similarly, can change the contents with an assignment Note: the contents are raw text, no HTML formatting Also: contents are accessed as a string, must parseFloat if want a number Fun with Text Boxes Enter a number here: <input type="button" name="double" value="Double" onClick="document.BoxForm.number.value= parseFloat(document.BoxForm.number.value) * 2;"> view page in browser

Text box events ONCHANGE triggered when the contents of the box are changed ONFOCUS triggered when the mouse clicks in the box blur() removes focus Fun with Text Boxes function FahrToCelsius(tempInFahr) // Assumes: tempInFahr is a number (degrees Fahrenheit) // Returns: corresponding temperature in degrees Celsius { return (5/9)*(tempInFahr - 32); } Temperature in Fahrenheit: <input type="text" name="Fahr" size=10 value="0" onChange="document.BoxForm.Celsius.value = FahrToCelsius(parseFloat(document.BoxForm.Fahr.value));"> ----> <input type="text" name="Celsius" size=10 value="" onFocus="blur();"> in Celsius view page in browser

Text box validation what if the user enters a non-number in the Fahrenheit box? solution: have the text box validate its own contents  start with legal value  at ONCHANGE, verify that new value is legal (otherwise, reset)  the verify.js library defines several functions for validating text boxes function verifyNum(textBox, resetValue) // Assumes: textBox is a text box, resetValue is optional // Results: alert if textBox does not contain a number, resets if provided { var boxValue = parseFloat(textBox.value); if ( isNaN(boxValue) ) { alert("You must enter a number value!"); if (resetValue != null) { textBox.value = resetValue; }

Validation example Fun with Text Boxes <script language="JavaScript" src=" function FahrToCelsius(tempInFahr) { return (5/9)*(tempInFahr - 32); } Temperature in Fahrenheit: <input type="text" name="Fahr" size=10 value=0 onChange="verifyNum(this, 0); // this refers to current element document.BoxForm.Celsius.value = FahrToCelsius(parseFloat(this.value));"> ----> in Celsius view page in browser

Text areas a TEXT box is limited to one line of input/output a TEXTAREA is similar to a text box in functionality, but can specify any number of rows and columns Initial Text  Note: unlike a text box, a TEXTAREA has closing tag initial contents of the TEXTAREA appear between the tags  WRAP="virtual" specifies that text in the box will wrap lines as needed  as with a text box, no HTML formatting of TEXTAREA contents

Textarea example Fun with Textareas function Table(low, high, power) // Results: displays table of numbers between low & high, raised to power { var message = "i: i^" + power + "\n \n"; for (var i = low; i <= high; i++) { message = message + i + ": " + Math.pow(i, power) + "\n"; } document.AreaForm.Output.value = message; } Show the numbers from to raised to the power of <input type="button" value="Generate Table" onClick="Table(parseFloat(document.AreaForm.lowRange.value), parseFloat(document.AreaForm.highRange.value), parseFloat(document.AreaForm.power.value));"> view page in browser

Textarea example refined Fun with Frames <script language="JavaScript" src=" function Table(low, high, power) { /* AS BEFORE */ } Show the numbers from <input type="text" name="lowRange" size=4 value=1 onChange="verifyInt(this, 1);"> to <input type="text" name="highRange" size=4 value=10 onChange="verifyInt(this, 10);"> raised to the power of <input type="text" name="power" size=3 value=2 onChange="verifyInt(this, 2);"> <input type="button" value="Generate Table" onClick="Table(parseFloat(document.AreaForm.lowRange.value), parseFloat(document.AreaForm.highRange.value), parseFloat(document.AreaForm.power.value));"> view page in browser

More examples Hoops tournament  text boxes in a table to form brackets  users selects teams by clicking on text boxes, automatically filling in brackets Letter sequence generator  text boxes to input sequence length, number of sequences, letter choices  button to initiate generation of sequences  text area to display sequences Substitution cipher  text box to enter substitution key  text areas for message & code, generates code at ONCHANGE event Prisoner's Dilemma simulation  select boxes for choosing strategies to compete  text boxes for results of each round, scores  buttons to play a single round, complete all rounds, reset Random walk simulator  text box to display position of walker, number of steps  button to initiate a step

JavaScript & frames Table of Squares alternatives for program output: 1.alert box : good for small messages 2.separate window : good for longer text, outside of page 3.text box / text area : integrated into page, but awkward & no formatting 4.frames : can easily write lots of output, integrated & fully formattable src="about:blank" loads a blank page into the frame (ready to be written to)

Frame example Fun with Frames function Help() // Results: displays a help message in a separate frame { parent.Output.document.open(); parent.Output.document.write("This might be a context-" + "sensitive help message, depending on the " + "application and state of the page."); parent.Output.document.close(); } view page in browser

Fun with Frames function Table(low, high, power) { parent.Output.document.open(); parent.Output.document.write(" i " + " i 2 "); for (var i = low; i <= high; i++) { parent.Output.document.write(" " + i + " " + " " + Math.pow(i, power) + " "); } parent.Output.document.write(" "); parent.Output.document.close(); } Show the numbers from to raised to the power of <input type="button" value="Generate Table" onClick="Table(parseFloat(document.ButtonForm.lowRange.value), parseFloat(document.ButtonForm.highRange.value), parseFloat(document.ButtonForm.power.value));"> Better example view page in browser

JavaScript & timeouts the setTimeout function can be used to execute code at a later time setTimeout(JavaScriptCodeToBeExecuted, MillisecondsUntilExecution) Example: forward link to a moved page Fun with Timeouts function Move() // Results: sets the current page contents to be newhome.html { self.location.href = "newhome.html"; } This page has moved to newhome.html. view page in browser

Fun with Timeouts function timeSince() // Assumes: document.CountForm.countdown exists in the page // Results: every second, recursively writes current countdown in the box { // CODE FOR DETERMINING NUMBER OF DAYS, HOURS, MINUTES, AND SECONDS // UNTIL GRADUATION document.CountForm.countdown.value= days + " days, " + hours + " hours, " + minutes + " minutes, and " + secs + " seconds"; setTimeout("timeSince();", 1000); } Countdown to Graduation 2002 Another timeout example view page in browser

Cookies & JavaScript recall that cookies are data files stored on the client machine  can be accessed and/or modified by the server  can also be accessed and/or modified directly by JavaScript code in a page potential applications:  e-commerce: remember customer name, past visits/purchases, password, …  tutorials: remember past experience, performance on quizzes, …  games: remember high score, best times, … each Web page can have its own cookie  document.cookie is a string of attribute=value pairs, separated by ; "userName=Dave;score=100;expires=Thu, 29-Feb-01 00:00:01 GMT"

function getCookie(Attribute) // Assumes: Attribute is a string // Results: gets the value stored under the Attribute { if (document.cookie.indexOf(Attribute+"=") == -1) { return ""; } else { var begin = document.cookie.indexOf(Attribute+"=") + Attribute.length+1; var end = document.cookie.indexOf(";", begin); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(begin, end)); } function setCookie(Attribute, Value) // Assumes: Attribute is a string // Results: stores Value under the name Attribute, expires in 30 days { var ExpireDate = new Date(); ExpireDate.setTime(ExpireDate.getTime() + (30*24*3600*1000)); document.cookie = Attribute + "=" + escape(Value) + "; expires=" + ExpireDate.toGMTString(); } function delCookie(Attribute) // Assumes: Attribute is a string // Results: removes the cookie value under the name Attribute { var now = new Date(); document.cookie = Attribute + "=; expires=" + now.toGMTString(); } cookie.js

Fun with Cookies <script language="JavaScript" src=" function Greeting() // Results: displays greeting using cookie { visitCount = getCookie("visits"); if (visitCount == "") { alert("Welcome to my page, newbie."); setCookie("visits", 1); } else { visitCount = parseFloat(visitCount)+1; alert("Welcome back for visit #" + visitCount); setCookie("visits", visitCount); } Here is the stuff in my page. Cookie example view page in browser

Next week… Java programming for the Web  design goals and language features  data types, controls statements, program structure  classes & objects  applications vs. applets read Chapters 19 & 20 as always, be prepared for a quiz on  today’s lecture (moderately thorough)  the reading (superficial)