JavaScript: Functions ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

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 Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
1 CSC 551: Web Programming Spring 2004 client-side programming with JavaScript  scripts vs. programs  JavaScript vs. JScript vs. VBScript  common tasks.
1 Programmer-Defined Functions Functions allow program modularization Variables declared in function are local variables Only known inside function in.
The Web Warrior Guide to Web Design Technologies
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Introduction to Scripting.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 7 Event-Driven.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Functions Part I.
1 JavaScript/Jscript 4 Functions. 2 Introduction Programs that solve real-world programs –More complex than programs from previous chapters Best way to.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together.
Introduction to scripting
4.1 JavaScript Introduction
CNIT 133 Interactive Web Pags – JavaScript and AJAX Event and Event Handling.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
WEB DESIGN AND PROGRAMMING Introduction to Javascript.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Bookstore Web Application Introducing Visual Web Developer 2008 Express and the.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Chapter 6: Forms JavaScript - Introductory. Previewing the Product Registration Form.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Overview of Previous Lesson(s) Over View  ASP.NET Pages  Modular in nature and divided into the core sections  Page directives  Code Section  Page.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
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.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Pemrograman Teknologi Internet W06: Functions and Events.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
JavaScript - A Web Script Language Fred Durao
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
 2001 Deitel & Associates, Inc. All rights reserved. 1 Outline 16.1Introduction 16.2Program Modules in JavaScript 16.3Programmer-Defined Functions 16.4Function.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved. 2 Revised by Dr. T. Tran for CSI3140.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 10: JavaScript Functions CIS 275—Web Application Development for Business I.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 13 GUI Programming.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
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.
Host Objects: Browsers and the DOM
Understanding JavaScript and Coding Essentials Lesson 8.
Document Object Model Nasrullah. DOM When a page is loaded,browser creates a Document Object Model of the Page.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
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.
1 Lesson 6 Introducing JavaScript HTML and JavaScript BASICS, 4 th Edition.
CSC 121 Computers and Scientific Thinking Fall Event-Driven Programming.
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
Internet & World Wide Web How to Program, 5/e.  JavaScript events  allow scripts to respond to user interactions and modify the page accordingly  Events.
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 5 Host Objects: Browsers.
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
REEM ALMOTIRI Information Technology Department Majmaah University.
Chapter 6 JavaScript: Introduction to Scripting
JavaScript Event Handling.
JavaScript: Functions
JavaScript Functions.
JavaScript: Functions.
Chapter 10 - JavaScript: Functions
Presentation transcript:

JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.

 To develop and maintain a large program  construct it from small, simple pieces  divide and conquer © by Pearson Education, Inc. All Rights Reserved.

 return statement  passes information from inside a function back to the point in the program where it was called  A function must be called explicitly for the code in its body to execute  The format of a function definition is function function-name( parameter-list ) { declarations and statements } © by Pearson Education, Inc. All Rights Reserved.

 Three ways to return control to the point at which a function was invoked  Reaching the function-ending right brace  Executing the statement return;  Executing the statement “ return expression; ” to return the value of expression to the caller  When a return statement executes, control returns immediately to the point at which the function was invoked © by Pearson Education, Inc. All Rights Reserved.

 The script in our next example (Fig. 9.3) uses a programmer-defined function called maximum to determine and return the largest of three floating-point values. © by Pearson Education, Inc. All Rights Reserved.

 random method generates a floating-point value from 0.0 up to, but not including, 1.0  Random integers in a certain range can be generated by scaling and shifting the values returned by random, then using Math.floor to convert them to integers  The scaling factor determines the size of the range (i.e. a scaling factor of 4 means four possible integers)  The shift number is added to the result to determine where the range begins (i.e. shifting the numbers by 3 would give numbers between 3 and 7)  Method Math.floor rounds its argument down to the closest integer © by Pearson Education, Inc. All Rights Reserved.

 In the next example, we build a random image generator—a script that displays four randomly selected die images every time the user clicks a Roll Dice button on the page.  For the script in Fig. 9.5 to function properly, the directory containing the file RollDice.html must also contain the six die images—these are included with this chapter’s examples. © by Pearson Education, Inc. All Rights Reserved.

User Interactions Via Event Handling  Until now, all user interactions with scripts have been through  a prompt dialog or  an alert dialog.  These dialogs are valid ways to receive input from a user and to display messages, but they’re fairly limited in their capabilities.  A prompt dialog can obtain only one value at a time from the user, and a message dialog can display only one message.  Inputs are typically received from the user via an HTML5 form.  Outputs are typically displayed to the user in the web page.  This program uses an HTML5 form and a new graphical user interface concept—GUI event handling.  The JavaScript executes in response to the user’s interaction with an element in a form. This interaction causes an event.  Scripts are often used to respond to user initiated events. © by Pearson Education, Inc. All Rights Reserved.

The body Element  The elements in the body are used extensively in the script. The form Element  The HTML5 standard requires that every form contain an action attribute, but because this form does not post its information to a web server, the string "#" is used simply to allow this document to validate.  The # symbol by itself represents the current page. © by Pearson Education, Inc. All Rights Reserved.

The button input Element and Event-Driven Programming  Event-driven programming  the user interacts with an element in the web page, the script is notified of the event and the script processes the event.  The user’s interaction with the GUI “drives” the program.  The button click is known as the event.  The function that’s called when an event occurs is known as an event handler.  When a GUI event occurs in a form, the browser calls the specified event-handling function.  Before any event can be processed, each element must know which event-handling function will be called when a particular event occurs. © by Pearson Education, Inc. All Rights Reserved.

The img Elements  The four img elements will display the four randomly selected dice.  Their id attributes ( die1, die2, die3 and die4, respectively) can be used to apply CSS styles and to enable script code to refer to these element in the HTML5 document.  Because the id attribute, if specified, must have a unique value among all id attributes in the page, Java- Script can reliably refer to any single element via its id attribute.  Each img element displays the image blank.png (an empty white image) when the page first renders. © by Pearson Education, Inc. All Rights Reserved.

Specifying a Function to Call When the Browser Finishes Loading a Document  Many examples will execute a JavaScript function when the document finishes loading.  This is accomplished by handling the window object’s load event.  To specify the function to call when an event occurs, you registering an event handler for that event.  Method addEventListener is available for every DOM node. The method takes three arguments:  the first is the name of the event for which we’re registering a handler  the second is the function that will be called to handle the event  the last argument is typically false—the true value is beyond this book’s scope © by Pearson Education, Inc. All Rights Reserved.