1 JavaScript Part 3. Functions Allow the user to decide when a particular script should be run by the browser in stead of running as long as the page.

Slides:



Advertisements
Similar presentations
1 CSC 551: Web Programming Spring 2004 client-side programming with JavaScript  scripts vs. programs  JavaScript vs. JScript vs. VBScript  common tasks.
Advertisements

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
Web Development & Design Foundations with XHTML Chapter 14 Key Concepts.
Introduction to JavaScript. Aim To enable you to write you first JavaScript.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together.
Javascript and the Web Whys and Hows of Javascript.
CSC318 – DYNAMIC WEB APPLICATION DEVELOPMENT
JavaScript Part 1.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
JavaScript is a client-side scripting language. Programs run in the web browser on the client's computer. (PHP, in contrast, is a server-side scripting.
Arrays – What is it? – Creation – Changing the contents Functions – What is it? – Syntax – How they work – Anonymous functions A quick lesson in Objects.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
Faculty of Sciences and Social Sciences HOPE JavaScript Advanced Stewart Blakeway FML
Using Client-Side Scripts to Enhance Web Applications 1.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 7 TH EDITION Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
Client-Side Scripting JavaScript.  produced by Netscape for use within HTML Web pages.  built into all the major modern browsers. properties  lightweight,
Dynamic Web Pages & JavaScript. Dynamic Web Pages Dynamic = Change Dynamic Web Pages are web pages that change. More than just moving graphics around.
Computers and Scientific Thinking David Reed, Creighton University Functions and Libraries 1.
XP Tutorial 6 New Perspectives on JavaScript, Comprehensive1 Working with Windows and Frames Enhancing a Web Site with Interactive Windows.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (there is an audio component to this eLesson) © Dr.
Copyright ©2005  Department of Computer & Information Science Introducing Dialogue Windows.
JavaScript, jQuery, and Mashups Incorporating JavaScript, jQuery, and other Mashups into existing pages.
JS Basics 1 Lecture JavaScript - Basics. JS Basics 2 What is JavaScript JavaScript is a “simple”, interpreted, programming language with elementary object-
Java Script User Defined Functions. Java Script  You can define your own JavaScript functions. Such functions are called user- defined, as opposed to.
THINKING BIG Abstraction and Functions chapter 6 Modified by Dr. Paul Mullins for CPSC 130, F05.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Input & Output Functions JavaScript is special from other languages because it can accept input and produce output on the basis of that input in the same.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
Scott Marino MSMIS Summer Session Web Site Design and Authoring Session 8 Scott Marino.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
JavaScript, Fourth Edition
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
HTML Overview Part 5 – JavaScript 1. Scripts 2  Scripts are used to add dynamic content to a web page.  Scripts consist of a list of commands that execute.
Making dynamic pages with javascript Lecture 1. Java script java versus javascript Javascript is a scripting language that will allow you to add real.
CIS 375—Web App Dev II JavaScript I. 2 Introduction to DTD JavaScript is a scripting language developed by ________. A scripting language is a lightweight.
Javascript JavaScript is what is called a client-side scripting language:  a programming language that runs inside an Internet browser (a browser is also.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 7 TH EDITION Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
Functions.  Assignment #2 is now due on Wednesday, Nov 25 th  (No Quiz)  Go over the midterm  Backtrack and re-cover the question about tracing the.
USING JAVASCRIPT TO SHOW AN ALERT Web Design Sec 6-2 Part or all of this lesson was adapted from the University of Washington’s “Web Design & Development.
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.
Event Handling (the right way). A Simple Web Page Events - Summary The web page looks like this:
Functions Function is a standalone block of statements that performs some tasks and returns a value. Functions must be declared before they can be used.
JavaScript Arrays, Functions, and Forms George Mason University.
Working with the Window Object JavaScript considers the browser window an object, which it calls the window object.
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.
Debugging No readings. 2 What's The Proper Indentation? function max(x,y) { if (x
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
JavaScript 101 Lesson 6: Introduction to Functions.
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
 The real power of PHP comes from its functions; it has more than 1000 built-in functions.  PHP User Defined Functions  Besides the built-in PHP functions,
INTERNET APPLICATIONS CPIT405 JavaScript Instructor: Rasha AlOmari
Web Programming Java Script-Introduction. What is Javascript? JavaScript is a scripting language using for the Web. JavaScript is a programming language.
WEB SYSTEMS & TECHNOLOGY. Java Script  JavaScript created by Netscape  It is also client side programming  It can be use for client side checks.
REEM ALMOTIRI Information Technology Department Majmaah University.
CHAPTER 10 JAVA SCRIPT.
Using JavaScript to Show an Alert
>> JavaScript: Location, Functions and Objects
Web Development & Design Foundations with HTML5
JAVA Script : Functions Ashima Wadhwa
JavaScript Loops.
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
Console.
Event Driven Programming & User Defined Functions
JavaScript MCS/BCS.
Web Development & Design Foundations with H T M L 5
JavaScript: Introduction to Scripting
Web Development & Design Foundations with HTML5 7th Edition
Presentation transcript:

1 JavaScript Part 3

Functions Allow the user to decide when a particular script should be run by the browser in stead of running as long as the page loads. 2

Functions A function is a block of one or more JavaScript statements with a specific purpose, which can be run when needed. Every function must be given a name, and can be invoked, or called, by other parts of the script. Function can be called as many times as needed during the running of the script. function function_name() {... JavaScript statements … } 3

Remember the form buttons? Three types of buttons: does not have any default action related to forms Or 4

Using Functions <!-- function showAlert() { alert(”Welcome to Our Site"); } //--> 5 Calling the Function (body) Defining the Function (head)

Same output <!-- function showAlert() { alert(”Welcome to Our Site"); } //--> 6

Hands-on practice 2 Remember the hand-on practice 2 last lecture? move the prompting script into a function with an onlick event handler to a button. Choose meaningful function name, e.g. promptQuantity() Code your JavaScript in the head section 7

The answer <!–- function promptQuantity() { var quantity; quantity = prompt("Type a quantity greater than 0"); if (quantity<=0) { document.write("quantity is not greater than 0,please refresh the web page"); } else{ document.write("Thank You!"); } //--> 8 <input type=“button” value=“click on enter a quantity” onclick=promptQuantity();>

Hands-on practice 3 Write the JavaScript code using a function to display an alert message for users who are under 18 years old and a different alert message for users who are 18 years or older. The function will be invoked using a button. 9

The Answer <!-- function showAlert() { var userAge userAge=prompt("type your age"); if (userAge< 18) { alert("You are under age!"); } if (userAge>=18) { alert("you are 18 or older");} } //--> You will be asked to type your age 10

Function Parameters When you call a function, you can pass values to it. These values are called arguments or parameters. Identifiers, in the function definition, are called parameters. Multiple parameters are separated by commas: function myFunction(parameter1, parameter2) { code to be executed } 11

Function Arguments Values received by the function, when the function is invoked, are called arguments. The parameters and the arguments must be in the same order: var x = myFunction(argument1, argument2); 12

Example: A function that will add one to a number entered by the user: <!-- function addOne(userNumber) { userNumber++; alert(userNumber); } //--> A function that adds 1 to the number input by the user <!-- var userNumber=prompt("Enter a number"); addOne(userNumber); //--> 13

Passing information to a function 14

Soft copy code JavaScript Practice <!-- function myfunction(message) { alert(message); } //--> testing buttons 15

The Return Statement When JavaScript reach a return statement, the function will stop executing. If the function was invoked from a JavaScript statement, JavaScript will "return" and continue to execute the code after the invoking statement. Functions often computes a return value. The return value is "returned" back to the "caller": var x = myFunction(4, 3); // Function is called, return value will end up in x function myFunction(a, b) { return a * b; // Function returns the product of a and b} 16

What is the output? In the next slide you have a function that requires two values entered by the user, lets assume that the user’s input were 10 as the width and 10 as the length, what would be the output? 17

What is the output? <!-- function Area(Width, Length) { var Size = Width*Length; return Size; } //--> <!-- var Width=prompt("enter the width"); var Length=prompt("enter the length"); var Size=Area(Width,Length); document.write(Size); //--> 18

Copyright © Terry Felke-Morris JavaScript tutorial 19