CSCE 102 – Chapter 9 (Functions and Parameters) CSCE 102 - General Applications Programming Benito Mendoza 1 2016-6-3 Benito Mendoza 1 By Benito Mendoza.

Slides:



Advertisements
Similar presentations
Chapter 08: Adding Adding Interactivity Interactivity With With Behaviors Behaviors By Bill Bennett Associate Professor MSJC CIS MVC.
Advertisements

The Web Warrior Guide to Web Design Technologies
Web-based Application Development Lecture 9 February 7, 2006 Anita Raja.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 7 Event-Driven.
Web-based Application Development Lecture 13 February 21, 2006 Anita Raja.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Web-based Application Development Lecture 18 March 21, 2006 Anita Raja.
Computer Science 103 Chapter 4 Advanced JavaScript.
JavaScript 101 Lesson 5: Introduction to Events. Lesson Topics Event driven programming Events and event handlers The onClick event handler for hyperlinks.
(CS1301) Introduction to Computer Programming City Univ of HK / Dept of CS / Helena Wong 1. Overview of Programming and JavaScript - 1
Web Programming Material From Greenlaw/Hepp, In-line/On-line: Fundamentals of the Internet and the World Wide Web 1 Introduction The JavaScript Programming.
1 Events Lect 8. 2 Event-driven Pages one popular feature of the Web is its interactive nature e.g., you click on buttons to make windows appear e.g.,
Programming Games Computer science big ideas. Computer Science jargon. Show virtual dog Homework: [Catch up: dice game, credit card or other form.] Plan.
Server-side Scripting Powering the webs favourite services.
Event Handlers CS101 Introduction to Computing. Learning Goals Learn about event handlers Determine how events are useful in JavaScript Discover where.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
© 2000 – All Rights Reserved - Page 1 Introduction to JavaScript Programming Part Two.
UFCEWT-20-3 Advanced Topics in Web Development Lecture 4 : JavaScript, Browsers & the HTML DOM-API.
Adding User Interactivity – Lesson 51 Adding User Interactivity Lesson 5.
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.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
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.
Manipulating the DOM CST 200 – JavaScript 3 –
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.
CSCE 102 – Chapter 6 (Web Design and Layout) CSCE General Applications Programming Benito Mendoza Benito Mendoza 1 By Benito Mendoza.
Functions and Parameters. Structured Programming.
Computers and Scientific Thinking David Reed, Creighton University Functions and Libraries 1.
JavaScript - A Web Script Language Fred Durao
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
ECA 225 Applied Interactive Programming ECA 225 Applied Online Programming images.
JavaScript - Basic Concepts Prepared and Presented by Hienvinh Nguyen, Afshin Tiraie.
1 JavaScript 2 JavaScript. 2 Rollovers Most web page developers first use JavaScript for rollovers A rollover is a change in the appearance of an element.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Lecture 15 ITIS 2300 Mid Term Review February 28, 2006 Dr. Anita Raja.
JQuery JavaScript is a powerful language but it is not always easy to work with. jQuery is a JavaScript library that helps with: – HTML document traversal.
Introduction To JavaScript. Putting it Together (page 11) All javascript must go in-between the script tags. All javascript must go in-between the script.
CSCE 102 – Chapter 11 (Performing Calculations) CSCE General Applications Programming Benito Mendoza Benito Mendoza 1 By Benito Mendoza.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
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.
1 CSC317/318 INTERNET PROGRAMING / DYNAMIC WEB APPLICATION DEVELOPMENT Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 7 TH EDITION Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
Document Object Model Nasrullah. DOM When a page is loaded,browser creates a Document Object Model of the Page.
LESSON : EVENTS AND FORM VALIDATION -JAVASCRIPT. EVENTS CLICK.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
CSCE 102 – Chapter 8 (Objects and Variables ) CSCE General Applications Programming Benito Mendoza Benito Mendoza 1 By Benito Mendoza.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
Adding Interactivity Comp 140 Fall Web 2.0 Major change in internet usage –From mostly static pages Text Graphics Simple links –To new paradigm.
The Web Wizard’s Guide To JavaScript Chapter 4 Image Swapping.
Web Programming Java Script-Introduction. What is Javascript? JavaScript is a scripting language using for the Web. JavaScript is a programming language.
Functions Wouldn’t it be nice to be able to bring up a new animal and paragraph without having to reload the page each time? We can! We can place the.
Tarik Booker CS 120 California State University, Los Angeles.
REEM ALMOTIRI Information Technology Department Majmaah University.
JavaScript and HTML Simple Event Handling 11-May-18.
© 2015, Mike Murach & Associates, Inc.
Intro to JavaScript CS 1150 Spring 2017.
Section 17.1 Section 17.2 Add an audio file using HTML
Functions Comp 205 Fall ‘17.
Web Development & Design Foundations with HTML5 7th Edition
Programming the Web using XHTML and JavaScript
JavaScript and HTML Simple Event Handling 19-Sep-18.
Event Driven Programming & User Defined Functions
Basic HTML and Embed Codes
The Web Wizard’s Guide To JavaScript
Chapter 7 Event-Driven Pages
Introduction to Programming and JavaScript
Web Programming and Design
Web Programming and Design
JavaScript and HTML Simple Event Handling 26-Aug-19.
JavaScript and HTML Simple Event Handling 4-Oct-19.
Presentation transcript:

CSCE 102 – Chapter 9 (Functions and Parameters) CSCE General Applications Programming Benito Mendoza Benito Mendoza 1 By Benito Mendoza Department of Computer Science & Engineering Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 2 Repetitive code Same code needed in more than one place in a script Type the code over and over Copy and paste - still not very efficient Script gets longer and longer What if you make a mistake? Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 3 We need a way to: Package that code in one place Refer to the package whenever/wherever Modularization Re-useable Self-contained Reduce script size Make it easier to find and correct errors or make changes later Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 4 Objects are modules Self-contained Data (properties) Code (methods) Re-useable Can invoke a method: At any point in a script Repeatedly Can we create our own methods? Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 5 Generally, a function is simply a group of one or more statements In JavaScript specifically, a function is A method … of the window object Functions are created by “declaring” them Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 6 Syntax for function declaration: function someName() { … JavaScript statements … } Reserved word Required Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 7 Using Functions Good practice to declare functions in the section Ensures browser “knows” of the function Use functions in the section “Calling” a function similar to calling a method except object name not required: someName() window.someName() Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 8 Using Functions … function someName() { … } … someName() … Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 9 Using Functions Ch09-Ex-02.html Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 10 Using Functions some HTML a function call some more HTML some HTML function statement 1 function statement 2 … some more HTML Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 11 Using Functions Any number of functions can be declared in one element (within the section) Functions are executed in the order in which they’re called, not the order in which they’re declared. Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 12 Parameters Parameter/argument: the means by which data is supplied to a method confirm(“Are you sure?”) ultraJava.changeGrind(“coarse”) Why parameters? General code is re-useable Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 13 Parameters function printGreeting() { alert(“Hello, Fred”) } function printGreeting() {alert(“Hello, Mary”)} function greetFred() { alert(“Hello, Fred”) } function greetMary() { alert(“Hello, Mary”) } Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 14 Parameters Need a printGreeting function that uses a parameter function printGreeting(personName) { alert(“Hello,” + personName) } Call by personName=“Fred” printGreeting(personName) Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 15 Parameters “Passing” a parameter Main program printGreeting var personName … personName=“Fred” … printGreeting(personName) … personName Fred Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 16 Parameters Ch09-Ex-05.html Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 17 Parameters Multiple parameters Declaring: function sample(a, b, c, d) Calling sample(“Bob”,”Mary”,user1, user2) Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 18 Parameters One-for-one correspondence between parameter order in declaration and in call Declaration: function sample(a, b, c, d) Call: sample(“Bob”,”Mary”,user1, user2) Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 19 Parameters Ch09-Ex-07.html Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 20 Image Objects ArrayArray conceptconcept Window object hierarchy Images are children of the document object Numbered: document.images[0] document.images[1] … document.images[n] Square brackets required Numbering begins with zero Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 21 Image Objects Images loaded in the order they appear in the HTML document Image numbers are assigned in the same order First image = document.images[0] Second image = document.images[1] Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 22 Image Objects Images have attributes: height width src Attribute references: document.images[0].width document.images[3].src Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 23 Image Objects Problem: referring to images by their object name is clumsy Have to figure out the order in which they’re loaded to determine the image’s number Using document.images[5] isn’t descriptive and makes the script harder to read and understand Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 24 Image Objects Solution: id attribute of the img tag Object reference: document.tower.width document.tower.src Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 25 Image Objects height and width properties are read-only Therefore, you can’t change them from JavaScript src property is read-write So: can’t change original image dimensions but you can replace it with another one Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 26 Image Objects … document.images[2].src=“eiffelnight.jpg” (or) document.tower.src=“eiffelnight.jpg” However, height and width of new image will be the same as the original image Assume this is the 3 rd image loaded Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 27 Image Objects Ch09-Ex-04.htm Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 28 Image Rollovers 1.Create an img tag with the original image 2.Create an element (link) that includes event handlers: onmouseover replaces original image onmouseout restores original image 3.When user hovers over link the image changes 4.When user leaves link the image changes back Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 29 Image Rollovers … … Click here for evening events Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 30 Image Rollovers function nightImage() { document.day_tower.src=“eiffelnight.jpg” } function dayImage() { document.day_tower.src=“eiffeltower.jpg ” } … <a href=“nightschedule.html” ¬ onmouseover=“nightImage()”¬ onmouseout=“dayImage()”> Click here for evening events Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 31 Image Rollovers Problem: All these images have to be downloaded to the users machine as they needed Solution: pre-loaded (pre-cached) images Pre-cached images are loaded at the same time as the other page content Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 32 Image Rollovers Process: Create an image object Load an image into that object Image object var nightimage = new image(69,120) Load image: nightimage.src = “night_tower.jpg” Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 33 Image Rollovers Example.html Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 34 Debugging Exercise Parameter Example function displayMessages(firstMsg, secondMsg, thirdMsg) alert(thirdMsg); alert("The first message is: firstMsg"); alert(The second message is: "secondMsg"); } This is a sample page that uses JavaScript functions and parameters. displaymessages("Ready?", "Let's go", "Not yet"); <input type="button" value="Click here to see some more messages" onclick="displaymessages("Here we go again", "What did you say?", "Time to go")"> Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 35 Debugging Exercise Parameter Example function displayMessages(firstMsg, secondMsg, thirdMsg) alert(thirdMsg) alert("The first message is: " + firstMsg) alert("The second message is: " + secondMsg) } This is a sample page that uses JavaScript functions and parameters. displayMessages("Ready?", "Let's go", "Not yet") onclick="displayMessages('Here we go again', 'What did you say?', 'Time to go')"> Divide and Conquer: Using Functions Adding a Delivery System: Parameters Creating Image Rollovers

CSCE 102 – Chapter 9 (Functions and Parameters) Benito Mendoza 36 Extra ndex.htm