JavaScript functions.

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

The Web Warrior Guide to Web Design Technologies
 Inside attributes like onclick  As a section of code in the body  Only special cases  As a function in a separate file  Can link to it like the.
29 November JavaScript: Arrays and Iterations Functions.
JAVASCRIPT TIPS. REMEMBER JAVASCRIPT IS VERY, VERY CASE SENSITIVE.
JavaScript Form Validation
CSC318 – DYNAMIC WEB APPLICATION DEVELOPMENT
GDT V5 Web Services. GDT V5 Web Services Doug Evans and Detlef Lexut GDT 2008 International User Conference August 10 – 13  Lake Las Vegas, Nevada GDT.
1 CLIENT-SIDE SCRIPTS. Objectives 2 Learn how to reference objects in HTML documents using the HTML DOM and dot syntax Learn how to create client-side.
SERVER web page repository WEB PAGE instructions stores information and instructions BROWSER retrieves web page and follows instructions Server Web Server.
Arrays – What is it? – Creation – Changing the contents Functions – What is it? – Syntax – How they work – Anonymous functions A quick lesson in Objects.
1 Forms and Form elements CSD 340 McCoey. 2 Form Object Properties action Usually a call to a server elements encoding method post or get target Methods.
1 Functions Lecfture Abstraction abstraction is the process of ignoring minutiae and focusing on the big picture in modern life, we are constantly.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
SERVER web page repository WEB PAGE instructions stores information and instructions BROWSER retrieves web page and follows instructions Server Web Server.
THINKING BIG Abstraction and Functions chapter 6 Modified by Dr. Paul Mullins for CPSC 130, F05.
JavaScript, Fourth Edition
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.
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.
COP 3813 Intro to Internet Computing Prof. Roy Levow Lecture 4 JavaScript.
JavaScript VARIABLES AND DATA TYPES. OUTPUT WITHOUT ALERT OR FORM CONSOLE.LOG();
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 Functions. CSS Inheritance Which formatting applies? x y z input { display: block; } input.pref { background:red; } If you have a selector.
 Labs next week:  Both labs will be posted on Monday  Both will be due the following Monday  Your decision on which to do first ▪ Web App Design Lab.
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.
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
Web Programming Java Script-Introduction. What is Javascript? JavaScript is a scripting language using for the Web. JavaScript is a programming language.
Client-side (JavaScript) Validation. Associating a function with a click event – Part 1 Use the input tag’s onclick attribute to associate a function.
 Collection of statements that can be invoked as a unit  Can take parameters  Can be used multiple times  Can call without knowing what they do or.
Precedence Operators Error Types
>> JavaScript: Location, Functions and Objects
Learning to Program D is for Digital.
JavaScript Wrap-up.
Example – SQL Injection
JavaScript.
Variables and Data Types
EXCEPTION HANDLING IN SERVER CLIENT PROGRAMMING
JavaScript functions.
Functions Comp 205 Fall ‘17.
JavaScript, continued.
JavaScript Functions.
JavaScript Part 2 Organizing JavaScript Code into Functions
JavaScript.
JavaScript Forms Adding User Input.
Web Programming– UFCFB Lecture 17
Higher-Order Procedures
Console.
CSS Colors, JavaScript Variables, Conditionals and Basic Methods
JavaScript Functions B. Ramamurthy 11/22/2018.
Javascript: variables and parameters
Event Driven Programming & User Defined Functions
JavaScript Functions.
Functions, Part 2 of 2 Topics Functions That Return a Value
Reviewing key concepts
JavaScript Programming Labs
Writing to a PAGE.
Programming in JavaScript
Programming in JavaScript
For this assignment, copy and past the XHTML to a notepad file with the .html extension. Then add the code I ask for to complete the problems.
Recognizing JavaScript Features
JavaScript Basics Topics Review Important Methods Writing Functions
Java Script Siddharth Srivastava.
Introduction to Programming and JavaScript
Writing to the Page Formatting Forms
CIS 136 Building Mobile Apps
Programming Basics Review
This is an introduction to JavaScript using the examples found at the CIS17 website. In previous examples I specified language = Javascript, instead of.
Writing to a PAGE.
Functions, Part 1 of 2 Topics Using Predefined Functions
Presentation transcript:

JavaScript functions

JavaScript functions Collection of statements that can be invoked as a unit Can take parameters Can be used multiple times Can call without knowing what they do or how

What we want to do

Form with input, button, output HTML

Add Data HTML

Push Button HTML

Fill in Output HTML

Form with input, button, output with a JavaScript function HTML JavaScript

Add data HTML JavaScript

Push button and input data sent to javascript HTML JavaScript

Javascript uses the data to create a new result HTML JavaScript

And moves it to the output location HTML JavaScript

Building Up Function Capabilities Return Parameters Function Cubby holes I can just read Cubby holes I can just read

SIMPLEST JAVASCRIPT FUNCTION Move the onclick work into a function

Function

Function format function name() { stmt; }

Moving onclick to a function <form name=“fname”> <button type=“button” onclick=“fname.output.value=‘Hi!’;”> Click </button> <input type=“text” name=“output”> </form> <form name=“fname”> <button type=“button” onclick=“doit();”> Click </button> <input type=“text” name=“output”> </form> Function doit() { fname.output.value=‘Hi!’; }

function name() { stmt; } Function format function name() { stmt; }

JAVASCRIPT FUNCTIONS WITH PARAMETERS Let the function work on any data

Parameters Function

function name(parm) { stmt; } Function format function name(parm) { stmt; }

Moving onclick to a function <form name=“fname”> <button type=“button” onclick=“fname.output.value=‘Hi!’+ Math.round(5*Math.random());”> Click </button> <input text=“type” name=“output”> </form> <form name=“fname”> <button type=“button” onclick=“doit();”> Click </button> <input text=“type” name=“output”> </form> Function doit() { var num = Math.round(5*Math.random()); fname.output.value=‘Hi!’+num; }

Parameter Name is a placeholder Can be used anywhere in a function Can be used as many times as you want Can not change it MUST supply value when call Can be different every time

parameters Just a special type of variable Something that you hand to the function Q: Many users: how do you name? A: Give it its OWN names to use locally Q: How do you match up? A: By POSITION

FUNCTION with parameters HTML <head> <script src=“function.js”></script> </head> <body> <button type=“button” onclick=“doit(3,5);”> </body> function doit (a,b) { var c = a*b); alert(“product is ”+c); } JAVASCRIPT (function.js)

Passing Parameters HTML JS mypet(‘cow’); mypet(‘dog’); function mypet(pet) { alert(‘my pet: ‘+pet); }

Multiple Parameters Order matters Need different names

Passing Parameters HTML JS mypet(‘Mutt’,’Jeff’); mypet(‘Jeff’,’Mutt’); function taller(big,small) { alert (big+’ is taller than ‘+ small); }

RETURNING A VALUE Let the result be placed anywhere

Return Parameters Function

Returning a value Use the function as a value form.field.value = function(parm1, parm2); difference = subtract(minuhend,subtrahend); Contrast with alert(string); append(form.field.value,’end’);

Return value in JavaScript Want to get information BACK to HTML With a return, the function has a VALUE Can be used anywhere you can use a constant or variable Alert Assignment statement

FUNCTION with return HTML <head> <script src=“function.js”></script> </head> <body> <button type=“button” onclick=“alert(doit(3,5));”> </body> function doit (a,b) { var c = a*b); return(c); } JAVASCRIPT (function.js)

SUMMARY

Building our own Need to define Inputs Outputs What to do

Inputs: read only These are the parameters Order matters Need a way to reference them Position 1, position 2, … Cubby holes Better to use meaningful names Each name is just a pointer to the cubby hole Inputs: read only

output: write once Use a RETURN statement A write-once cubby hole Only way to access is the RETURN statement Once you set it, the function is ended Can have a simple value or more (e.g., concatenating strings) output: write once

WHAT TO DO Series of statements: the recipe Can use Assignment statements Function calls Can use Literals (5, “ “) parameters Specially defined locations (variables) WHAT TO DO