JavaScript Functions.

Slides:



Advertisements
Similar presentations
JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
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.
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: 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.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
CSS Class 7 Add JavaScript to your page Add event handlers Validate a form Open a new window Hide and show elements Swap images Debug JavaScript.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
INTRODUCTION TO HTML5 Using jQuery with HTML5. Introducing jQuery  Although it is not a part of any W3C or WHATWG specification, jQuery performs an important.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
 2001 Deitel & Associates, Inc. All rights reserved. 1 Outline 16.1Introduction 16.2Program Modules in JavaScript 16.3Programmer-Defined Functions 16.4Function.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
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.
JavaScript, Fourth Edition
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
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.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
Introduction to JavaScript Events Instructor: Sergey Goldman 1.
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.
REEM ALMOTIRI Information Technology Department Majmaah University.
Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016.
Introduction to.
Build in Objects In JavaScript, almost "everything" is an object.
Programming Web Pages with JavaScript
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Unit M Programming Web Pages with
Functions and an Introduction to Recursion
Introduction to JavaScript Events
Data Virtualization Tutorial… CORS and CIS
JavaScript Event Handling.
© 2016 Pearson Education, Ltd. All rights reserved.
Intro to JavaScript CS 1150 Spring 2017.
Project 9 Creating Pop-up Windows, Adding Scrolling Messages, and Validating Forms.
JavaScript: Functions
CSC113: Computer Programming (Theory = 03, Lab = 01)
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
JavaScript: Functions.
14 A Brief Look at JavaScript and jQuery.
Chapter 10 - JavaScript: Functions
Introduction to JavaScript
Chapter 5 - Functions Outline 5.1 Introduction
JavaScript Introduction
Objectives Insert a script element Write JavaScript comments
A second look at JavaScript
Event Driven Programming & User Defined Functions
WEB PROGRAMMING JavaScript.
PHP.
JavaScript: Functions
Introduction to JavaScript
JavaScript Basics Topics Review Important Methods Writing Functions
Introduction to Programming and JavaScript
CNIT 133 Interactive Web Pags – JavaScript and AJAX
Web Programming and Design
Computers and Scientific Thinking David Reed, Creighton University
Introduction to Web programming
Presentation transcript:

JavaScript Functions

Introduction JavaScript provides several objects that have a rich collection of methods for performing common mathematical calculations, string manipulations, date and time manipulations, and manipulations of collections of data called arrays. We can write functions to define tasks that may be used at many points in a script. These are referred to as programmer-defined functions. The actual statements defining the function are written only once and are hidden from other functions. A function is invoked (that is, made to perform its designated task) by a function call. The function call specifies the function name and provides information (as arguments) that the called function needs to perform its task. Functions are invoked by writing the name of the function, followed by a left parenthesis, followed by a comma-separated list of zero or more arguments, followed by a right parenthesis.

General Format of a Function Definition The function-name is any valid identifier. The parameter-list is a comma-separated list containing the names of the parameters received by the function when it’s called. If a function does not receive any values, the parameter-list is empty. The declarations and statements between the braces form the function body. Returning Program Control from a Function Definition If the function does not return a result, control returns when the program reaches the function ending right brace (}) or executes the statement If the function does return a result, the statement returns the value of expression to the caller When a return statement executes, control returns immediately to the point at which the function was invoked.

Programmer-Defined Function square

Programmer-Defined Function maximum

Random Number Generation Method random generates a floating-point value from 0.0 up to, but not including, 1.0. Scaling and Shifting Random Numbers

Displaying Random Images

The form Element 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.

The button input Element and Event-Driven Programming Line 54 defines a button input element with the id "rollButton" and containing the value Roll Dice which is displayed on the button. In this example, clicking the button will call function rollDice. This style of programming is known as 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 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.

The img Elements The four img elements (lines 57–60) 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. Each img element displays the image blank.png (an empty white image) when the page first renders. Specifying a Function to Call When the Browser Finishes Loading a Document All programs will execute a JavaScript function when the document finishes loading in the web browser window. 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. We register the window’s load event handler at line 49. Method addEventListener is available for every DOM node. window.addEventListener( "load", start, false );

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 Function start When the window’s load event occurs, function start registers the Roll Dice button’s click event handler (lines 23–24), which instructs the browser to listen for events If no event handler is specified for the Roll Dice button, the script will not respond when the user presses the button. The document object’s getElement-ById method, which, given an HTML5 element’s id as an argument, finds the element with the matching id attribute and returns a JavaScript object representing the element.

Function rollDice Function setImage The user clicks the Roll Dice button to roll the dice. This event invokes function rollDice (lines 32–38) in the script. Lines 34–37 call function setImage (lines 41–47) to randomly select and set the image for a specified img element. Function setImage Function setImage (lines 41–47) receives one parameter (dieImg) that represents the specific img element in which to display a randomly selected image. Line 43 picks a random integer from 1 to 6. Line 44 demonstrates how to access an img element’s src attribute programmatically in JavaScript. setAttribute method allows you to change the values of most of the HTML5 element’s attributes. In this case, we change the src attribute of the img element referred to by dieImg.

Rolling Dice Repeatedly and Displaying Statistics

Scope Rules Each identifier in a program also has a scope. The scope of an identifier for a variable or function is the portion of the program in which the identifier can be referenced. Global variables or script-level variables that are declared in the head element are accessible in any part of a script and are said to have global scope. Identifiers declared inside a function have function (or local) scope and can be used only in that function. Function scope begins with the opening left brace ({) of the function in which the identifier is declared and ends at the function’s terminating right brace (}). Local variables of a function and function parameters have function scope. If a local variable in a function has the same name as a global variable, the global variable is “hidden” from the body of the function.

JavaScript Global Functions

Recursion A recursive function is a function that calls itself, either directly, or indirectly through another function.