Lecture 8: Events and timers

Slides:



Advertisements
Similar presentations
The Document Object Model (DOM) 1 JavaScript is an object-based language—that is, it’s based on manipulating objects by changing each object’s properties.
Advertisements

HTML – A Brief introduction Title of page This is my first homepage. This text is bold  The first tag in your HTML document is. This tag tells your browser.
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.
The Web Warrior Guide to Web Design Technologies
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.
Tutorial 6 Working with Web Forms
Chapter 16 Dynamic HTML and Animation The Web Warrior Guide to Web Design Technologies.
Javascript Document Object Model. How to use JavaScript  JavaScript can be embedded into your html pages in a couple of ways  in elements in both and.
Computing Concepts Advanced HTML: Tables and Forms.
Form Basics CS Web Data  Most interesting web pages revolve around data  examples: Google, IMDB, Digg, Facebook, YouTube, Rotten Tomatoes  can.
JavaScript ICW: Lecture 11 Tom Chothia. Last Lecture URLs Threads, to make a process run in parallel: Make it extend Thread Give it a run method Call.
The Browser Object Model (BOM) The API of JavaScript’s browser host environment SE-2840 Dr. Mark L. Hornick 1.
JavaScript, Third Edition
Introduction to scripting
Web Development & Design Foundations with XHTML Chapter 9 Key Concepts.
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.
HTML. What is HTML? HTML is a language for describing web pages. HTML stands for Hyper Text Markup Language HTML is a markup language A markup language.
DAT602 Database Application Development Lecture 14 HTML.
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.
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.
XP New Perspectives on Browser and Basics Tutorial 1 1 Browser and Basics Tutorial 1.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
Unobtrusive JavaScript
Chapter 8 OBJECT-ORIENTED TECHNOLOGIES AND DSS DESIGN Decision Support Systems For Business Intelligence.
UFCEWT-20-3 Advanced Topics in Web Development Lecture 4 : JavaScript, Browsers & the HTML DOM-API.
CSE 154 LECTURE 19: EVENTS AND TIMERS. Anonymous functions function(parameters) { statements; } JS JavaScript allows you to declare anonymous functions.
 2008 Pearson Education, Inc. All rights reserved Introduction to XHTML Pt. 2.
CITS1231 Web Technologies JavaScript and Document Object Model.
Tutorial 7 Creating Forms. Objectives Session 7.1 – Create an HTML form – Insert fields for text – Add labels for form elements – Create radio buttons.
Lesson13. JavaScript JavaScript is an interpreted language, designed to function within a web browser. It can also be used on the server.
JavaScript, Fourth Edition
Using Client-Side Scripts to Enhance Web Applications 1.
Introduction to JavaScript 41 Introduction to Programming the WWW I CMSC Winter 2004 Lecture 17.
 2008 Pearson Education, Inc. All rights reserved Introduction to XHTML.
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.
 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing.
Chapter 5: Windows and Frames
XP Tutorial 6 New Perspectives on JavaScript, Comprehensive1 Working with Windows and Frames Enhancing a Web Site with Interactive Windows.
CSE 154 Lecture 20: The DoM tree.
1 HTML Forms
Web Development 101 Presented by John Valance
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.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
JavaScript, Fourth Edition Chapter 4 Manipulating the Browser Object Model.
SYST Web Technologies SYST Web Technologies XHTML Forms.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
Topic 5 Windows and Frames. 2 Goals and Objectives Goals Goals Understand JavaScript window object, how popup windows work, find the browser that a client.
20-753: Fundamentals of Web Programming 1 Lecture 6: Advanced HTML Fundamentals of Web Programming Lecture 6: Advanced HTML.
CSE 154 LECTURE 7: THE DOCUMENT OBJECT MODEL (DOM); UNOBTRUSIVE JAVASCRIPT.
Adding Javascript How to include javascript in a webpage.
JavaScript and Ajax (JavaScript window object) Week 6 Web site:
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 15 – Dynamic HTML: Object Model and Collections Outline 15.1Introduction 15.2Object Referencing.
CSE 154 LECTURE 9: THE DOM TREE. The DOM tree The elements of a page are nested into a tree-like structure of objects the DOM has properties and methods.
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.
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.
HTML Tables Tables are defined with the tag. A table is divided into rows (with the tag), and each row is divided into data cells (with the tag). td stands.
FORMS Explained By: Jasdeep Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
COM621: Advanced Interactive Web Development Lecture 6 – JavaScript (cont.)
Chapter 4 Java Script – Part2. 2 Location object Properties Properties href – whole path will be displayed. ex: window.location.href ( see example 11)
XHTML Forms.
Introduction to.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Lecture 9: Events and timers
Lecture 7: Unobtrusive JavaScript
Lecture 8: Events and timers
Lecture 7: Unobtrusive JavaScript
Presentation transcript:

Lecture 8: Events and timers CSE 154 Lecture 8: Events and timers

attribute

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)

More about form controls <select id="captain"> <option value="kirk">James T. Kirk</option> <option value="picard">Jean-Luc Picard</option> <option value="cisco">Benjamin Cisco</option> </select> <label> <input id="trekkie" type="checkbox" /> I'm a Trekkie </label> HTML output when talking to a text box or select, you usually want its value when talking to a checkbox or radio button, you probably want to know if it's checked (true/false)

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)

The six global DOM objects Every Javascript program can refer to the following global objects: name description document current HTML page and its content history list of pages the user has visited location URL of the current HTML page navigator info about the web browser you are using screen info about the screen area occupied by the browser window the browser window

the entire browser window; the top-level object in DOM hierarchy The window object technically, all global code and variables become part of the window object properties: document, history, location, name methods: alert, confirm, prompt (popup boxes) setInterval, setTimeout clearInterval, clearTimeout (timers) open, close (popping up new browser windows) blur, focus, moveBy, moveTo, print, resizeBy, resizeTo, scrollBy,  scrollTo the entire browser window; the top-level object in DOM hierarchy

Popup windows with window.open window.open("http://foo.com/bar.html", "My Foo Window", "width=900,height=600,scrollbars=1"); JS window.open pops up a new browser window THIS method is the cause of all the terrible popups on the web! some popup blocker software will prevent this method from running

the current web page and the elements inside it The document object the current web page and the elements inside it properties: anchors, body, cookie, domain, forms, images, links, referrer, title, URL methods: getElementById getElementsByName, getElementsByTagName querySelector, querySelectorAll close, open, write, writeln

the URL of the current web page The location object the URL of the current web page properties: host, hostname, href, pathname, port, protocol, search methods: assign, reload, replace

information about the web browser application The navigator object information about the web browser application properties: appName, appVersion, browserLanguage, cookieEnabled, platform, us erAgent Some web programmers examine the navigator object to see what browser is being used, and write browser-specific scripts and hacks: if (navigator.appName === "Microsoft Internet Explorer") { ... JS (this is poor style; you usually do not need to do this)

information about the client's display screen The screen object information about the client's display screen properties: availHeight, availWidth, colorDepth, height, pixelDepth, width

the list of sites the browser has visited in this window The history object the list of sites the browser has visited in this window properties: length methods: back, forward, go sometimes the browser won't let scripts view history properties, for security