CSE 154 LECTURE 19: EVENTS AND TIMERS. Anonymous functions function(parameters) { statements; } JS JavaScript allows you to declare anonymous functions.

Slides:



Advertisements
Similar presentations
Chapter 7 JavaScript: Introduction to Scripting. Outline Simple Programs Objects and Variables Obtaining User Input with prompt Dialogs – –Dynamic Welcome.
Advertisements

JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
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 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.
20-Jun-15 JavaScript and HTML Simple Event Handling.
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
XP Tutorial 1 New Perspectives on JavaScript, Comprehensive1 Introducing JavaScript Hiding Addresses from Spammers.
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.
Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
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.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
JavaScript & DOM Client-side scripting. JavaScript JavaScript is a tool to automate client side (which is implemented using HTML so far) JavaSript syntax.
Lesson 19. JavaScript errors Since JavaScript is an interpreted language, syntax errors will usually cause the script to fail. Both browsers will provide.
CITS1231 Web Technologies JavaScript and Document Object Model.
JavaScript, Fourth Edition
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Tutorial 10 Programming with JavaScript
Using Client-Side Scripts to Enhance Web Applications 1.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Introduction to JavaScript 41 Introduction to Programming the WWW I CMSC Winter 2004 Lecture 17.
CSE 154 LECTURE 18: THE DOCUMENT OBJECT MODEL (DOM); UNOBTRUSIVE JAVASCRIPT.
 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.
ECA 225 Applied Interactive Programming ECA 225 Applied Online Programming images.
Web Development 101 Presented by John Valance
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
Introduction to JavaScript Objects, Properties, Methods.
ASP. What is ASP? ASP stands for Active Server Pages ASP is a Microsoft Technology ASP is a program that runs inside IIS IIS stands for Internet Information.
JavaScript, Fourth Edition Chapter 4 Manipulating the Browser Object Model.
1 Javascript DOM Peter Atkinson. 2 Objectives Understand the nature and structure of the DOM Add and remove content from the page Access and change element.
Jaana Holvikivi 1 Introduction to Javascript Jaana Holvikivi Metropolia.
JavaScript Challenges Answers for challenges
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.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
Project 5: Using Pop-Up Windows Essentials for Design JavaScript Level One Michael Brooks.
Lecture 8: Events and timers
CISW CRC - Fishman 2014 / 2015 PRESENTATION 4b: CISW 400 (Online) – Client-side Scripting for the Internet Cosumnes River College Fishman – 2015.
Events CS The keyword this  all JavaScript code actually runs inside of an object  by default, code runs inside the global window object  all.
CSE 154 LECTURE 7: THE DOCUMENT OBJECT MODEL (DOM); UNOBTRUSIVE JAVASCRIPT.
Working with the Window Object JavaScript considers the browser window an object, which it calls the window object.
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.
JavaScript and Ajax (JavaScript Functions) Week 5 Web site:
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
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.
CSE 154 LECTURE 10: MORE EVENTS. Problems with reading/changing styles Click Me HTML window.onload = function() { document.getElementById("clickme").onclick.
COM621: Advanced Interactive Web Development Lecture 6 – JavaScript (cont.)
Introduction to.
Programming Web Pages with JavaScript
Applied Component I Unit II Introduction of java-script
Using DHTML to Enhance Web Pages
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Lecture 7: The Document Object Model (DOM); Unobtrusive JavaScript
JavaScript: Good Practices
Tutorial 10 Programming with JavaScript
14 A Brief Look at JavaScript and jQuery.
Lecture 7: Unobtrusive JavaScript
Lecture 8: Events and timers
Lecture 7: Unobtrusive JavaScript
Presentation transcript:

CSE 154 LECTURE 19: EVENTS AND TIMERS

Anonymous functions function(parameters) { statements; } JS JavaScript allows you to declare anonymous functions quickly creates a function without giving it a name can be stored as a variable, attached as an event handler, etc.

Anonymous function example window.onload = function() { var ok = document.getElementById("ok"); ok.onclick = okayClick; }; function okayClick() { alert("booyah"); } JS output or the following is also legal (though harder to read and bad style): window.onload = function() { document.getElementById("ok").onclick = function() { alert("booyah"); };

The danger of global variables var count = 0; function incr(n) { count += n; } function reset() { count = 0; } incr(4); incr(2); console.log(count); JS globals can be bad; other code and other JS files can see and modify them How many global symbols are introduced by the above code? 3 global symbols: count, incr, and reset

Enclosing code in a function function everything() { var count = 0; function incr(n) { count += n; } function reset() { count = 0; } incr(4); incr(2); console.log(count); } everything(); // call the function to run the code the above example moves all the code into a function; variables and functions declared inside another function are local to it, not global How many global symbols are introduced by the above code? 1 global symbol: everything (can we get it down to 0?)

The "module pattern" (function() { statements; })(); JS wraps all of your file's code in an anonymous function that is declared and immediately called 0 global symbols will be introduced! the variables and functions defined by your code cannot be messed with externally

Module pattern example (function() { var count = 0; function incr(n) { count += n; } function reset() { count = 0; } incr(4); incr(2); console.log(count); })(); JS How many global symbols are introduced by the above code? 0 global symbols

Unobtrusive styling function okayClick() { this.style.color = "red"; this.className = "highlighted"; } JS.highlighted { color: red; } CSS well-written JavaScript code should contain as little CSS as possible use JS to set CSS classes/IDs on elements define the styles of those classes/IDs in your CSS file

JavaScript "strict" mode "use strict"; your code... writing "use strict"; at the very top of your JS file turns on strict syntax checking: shows an error if you try to assign to an undeclared variable stops you from overwriting key JS system libraries forbids some unsafe or error-prone language features You should always turn on strict mode for your code in this class!

The six global DOM objects Every Javascript program can refer to the following global objects: namedescription documentcurrent HTML page and its content historylist of pages the user has visited locationURL of the current HTML page navigatorinfo about the web browser you are using screeninfo about the screen area occupied by the browser windowthe browser window

The window object the entire browser window; the top-level object in DOM hierarchy 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

Popup windows with window.open window.open(" "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 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 location object the URL of the current web page properties: host, hostname, href, pathname, port, protocol, search methods: assign, reload, replace

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)

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

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

Setting a timer methoddescription 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 Click me! 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!