Lecture 8: Events and timers

Slides:



Advertisements
Similar presentations
Table, List, Blocks, Inline Style
Advertisements

Tutorial 6 Creating a Web Form
DOM and timers CS Problems with JavaScript JavaScript is a powerful language, but it has many flaws:  the DOM can be clunky to use  the same code.
1 Introduction to HTML II อุทัย เซี่ยงเจ็น สำนักวิชาเทคโนโลยีสารสนเทศ และการสื่อสาร มหาวิทยาลัยนเรศวร พะเยา.
Images, Tables, lists, blocks, layout, forms, iframes
Chapter 6 Basic forms 1. Forms and query string 2 form : a group of user input (UI) controls that accepts information from the user and sends the information.
SE-2840 Dr. Mark L. Hornick 1 HTML input elements and forms.
Form Basics CS Web Data  Most interesting web pages revolve around data  examples: Google, IMDB, Digg, Facebook, YouTube, Rotten Tomatoes  can.
USER INTERACTIONS: FORMS
XHTML Forms for Data Collection and Submission Chapter 5.
User Interface Design using jQuery Mobile CIS 136 Building Mobile Apps 1.
More Event-Driven Programming Chapter Exercise Make a button whose label toggles between "on" and "off".
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.
CSE 154 LECTURE 9: FORMS. Web data most interesting web pages revolve around data examples: Google, IMDB, Digg, Facebook, YouTube, Rotten Tomatoes can.
Tutorial #9 – Creating Forms. Tutorial #8 Review – Tables Borders (table, gridlines), Border-collapse: collapse; empty-cells: show; and rowspan, colspan.
DOM and timers CS Problems with JavaScript JavaScript is a powerful language, but it has many flaws:  the DOM can be clunky to use  the same code.
CSE 154 LECTURE 19: EVENTS AND TIMERS. Anonymous functions function(parameters) { statements; } JS JavaScript allows you to declare anonymous functions.
Suzanne J. Sultan Javascript Lecture 2. Suzanne J. Sultan function What is a function> Types of functions:  Function with no parameters  Function with.
Tutorial 7 Creating Forms. Objectives Session 7.1 – Create an HTML form – Insert fields for text – Add labels for form elements – Create radio buttons.
INTRODUCTORY Tutorial 8 Creating Forms. XP New Perspectives on Blended HTML, XHTML, and CSS2 Objectives Create an HTML form Create fields for text Create.
© 2010 Delmar, Cengage Learning Chapter 8 Collecting Data with Forms.
CSE 154 LECTURE 18: THE DOCUMENT OBJECT MODEL (DOM); UNOBTRUSIVE JAVASCRIPT.
 Whether using paper forms or forms on the web, forms are used for gathering information. User enter information into designated areas, or fields. Forms.
CSE 154 Lecture 20: The DoM tree.
HTML Forms. Slide 2 Forms (Introduction) The purpose of input forms Organizing forms with a and Using different element types to get user input A brief.
HTML Forms a form is a container for input elements on a web page input elements contain data that is sent to the web server for processing.
SYST Web Technologies SYST Web Technologies XHTML Forms.
+ FORMS HTML forms are used to pass data to a server. begins and ends a form Forms are made up of input elements Every input element has a name and value.
Lecture 8: Events and timers
20-753: Fundamentals of Web Programming 1 Lecture 6: Advanced HTML Fundamentals of Web Programming Lecture 6: Advanced HTML.
USABLEANDACCESSIBLEFORMS. accessibility or Accessibility?
CSE 154 LECTURE 7: THE DOCUMENT OBJECT MODEL (DOM); UNOBTRUSIVE JAVASCRIPT.
Check Boxes. 2 Check boxes  Image from:  HTML:  Each box gets it's own.
Tutorial 6 Creating a Web Form
HTML FORM. Form HTML Forms are used to select different kinds of user input. HTML forms are used to pass data to a server. A form can contain input elements.
Lesson 5 Introduction to HTML Forms. Lesson 5 Forms A form is an area that can contain form elements. Form elements are elements that allow the user to.
FORMS Explained By: Jasdeep Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
XHTML Forms.
Tutorial 6 Working with Web Forms
Creating and Processing Web Forms
Introduction to.
Web Forms.
Web Forms.
Checkboxes, Select boxes, Radio buttons,
>> Introduction to CSS
Objectives Design a form Create a form Create text fields
CS3220 Web and Internet Programming HTML Tables and Forms
HTML Forms Pat Morin COMP 2405.
>> More on HTML Forms
(and available form fields)
Server-Side Application and Data Management IT IS 3105 (Spring 2010)
Intro to CSS CS 1150 Fall 2016.
Lecture 9: Events and timers
Programming the Web using XHTML and JavaScript
HTML/XHTML Forms 18-Sep-18.
Introducing Forms.
HTML Forms and User Input
Objectives Explore web forms Work with form servers
Intro to CSS CS 1150 Spring 2017.
Intro to CSS Mr. Singh.
Forms, cont’d.
In Class Programming BIS1523 – Lecture 11.
CNIT 131 HTML5 - Forms.
CS3220 Web and Internet Programming HTML Tables and Forms
Lecture 9: TIMERS and The DoM tree
Lecture 7: Unobtrusive JavaScript
CMPT241 Web Programming Chapter 6 Forms.
Lecture 7: Unobtrusive JavaScript
Web Forms.
Presentation transcript:

Lecture 8: Events and timers CSc 337 Lecture 8: Events and timers

Setting a timer method description setTimeout(function, delayMS); arranges to call given function after given delay in ms setInterval(function, delayMS); arranges to call function repeatedly every delayMS ms clearTimeout(timerID);  clearInterval(timerID); stops the given timer both setTimeout and setInterval return an ID representing the timer this ID can be passed to clearTimeout/Interval later to stop the timer

setTimeout example <button id="clickme">Click me!</button> <span id="output"></span> HTML window.onload = function() { document.getElementById("clickme").onclick = delayedMessage; }; function delayedMessage() { document.getElementById("output").innerHTML = "Wait for it..."; setTimeout(sayBooyah, 5000); } function sayBooyah() { // called when the timer goes off document.getElementById("output").innerHTML = "BOOYAH!"; } JS output

setInterval example var timer = null; // stores ID of interval timer function delayMsg2() { if (timer === null) { timer = setInterval(rudy, 1000); } else { clearInterval(timer); timer = null; } function rudy() { // called each time the timer goes off document.getElementById("output").innerHTML += " Rudy!"; } JS output

Passing parameters to timers function delayedMultiply() { // 6 and 7 are passed to multiply when timer goes off setTimeout(multiply, 2000, 6, 7); } function multiply(a, b) { alert(a * b); } JS output any parameters after the delay are eventually passed to the timer function doesn't work in IE; must create an intermediate function to pass the parameters why not just write this? setTimeout(multiply(6 * 7), 2000); JS

Common timer errors many students mistakenly write () when passing the function setTimeout(booyah(), 2000); setTimeout(booyah, 2000); setTimeout(multiply(num1 * num2), 2000); setTimeout(multiply, 2000, num1, num2); JS what does it actually do if you have the () ? it calls the function immediately, rather than waiting the 2000ms!

Checkboxes: <input> yes/no choices that can be checked and unchecked (inline) <input type="checkbox" name="lettuce" /> Lettuce <input type="checkbox" name="tomato" checked="checked" /> Tomato <input type="checkbox" name="pickles" checked="checked" /> Pickles HTML output none, 1, or many checkboxes can be checked at same time when sent to server, any checked boxes will be sent with value on: http://webster.cs.washington.edu/params.php?tomato=on&pickles=on use checked="checked" attribute in HTML to initially check the box

Radio buttons: <input> sets of mutually exclusive choices (inline) <input type="radio" name="cc" value="visa" checked="checked" /> Visa <input type="radio" name="cc" value="mastercard" /> MasterCard <input type="radio" name="cc" value="amex" /> American Express HTML output grouped by name attribute (only one can be checked at a time) must specify a value for each one or else it will be sent as value on

Text labels: <label> <label><input type="radio" name="cc" value="visa" checked="checked" /> Visa</label> <label><input type="radio" name="cc" value="mastercard" /> MasterCard</label> <label><input type="radio" name="cc" value="amex" /> American Express</label> HTML output associates nearby text with control, so you can click text to activate control can be used with checkboxes or radio buttons label element can be targeted by CSS style rules

Drop-down list: <select>, <option> menus of choices that collapse and expand (inline) <select name="favoritecharacter"> <option>Jerry</option> <option>George</option> <option selected="selected">Kramer</option> <option>Elaine</option> </select> HTML output option element represents each choice select optional attributes: disabled, multiple, size optional selected attribute sets which one is initially chosen

Using <select> for lists <select name="favoritecharacter[]" size="3" multiple="multiple"> <option>Jerry</option> <option>George</option> <option>Kramer</option> <option>Elaine</option> <option selected="selected">Newman</option> </select> HTML output optional multiple attribute allows selecting multiple items with shift- or ctrl-click must declare parameter's name with [] if you allow multiple selections option tags can be set to be initially selected

Option groups: <optgroup> <select name="favoritecharacter"> <optgroup label="Major Characters"> <option>Jerry</option> <option>George</option> <option>Kramer</option> <option>Elaine</option> </optgroup> <optgroup label="Minor Characters"> <option>Newman</option> <option>Susan</option> </select> HTML output What should we do if we don't like the bold appearance of the optgroups?

Grouping input: <fieldset>, <legend> groups of input fields with optional caption (block) <fieldset> <legend>Credit cards:</legend> <input type="radio" name="cc" value="visa" checked="checked" /> Visa <input type="radio" name="cc" value="mastercard" /> MasterCard <input type="radio" name="cc" value="amex" /> American Express </fieldset> HTML output fieldset groups related input fields, adds a border; legend supplies a caption

Styling form controls element[attribute="value"] { property : value; ... } CSS input[type="text"] { background-color: yellow; font-weight: bold; } CSS output attribute selector: matches only elements that have a particular attribute value useful for controls because many share the same element (input)

The innerHTML property <button onclick="addText();">Click me!</button> <span id="output">Hello </span> HTML function addText() { var span = document.getElementById("output"); span.innerHTML += " bro"; } JS output can change the text inside most elements by setting the innerHTML property

Abuse of innerHTML // bad style! var paragraph = document.getElementById("welcome"); paragraph.innerHTML = "<p>text and <a href=\"page.html\">link</a>"; JS innerHTML can inject arbitrary HTML content into the page however, this is prone to bugs and errors and is considered poor style we forbid using innerHTML to inject HTML tags; inject plain text only (later, we'll see a better way to inject content with HTML tags in it)