JavaScript functions.

Slides:



Advertisements
Similar presentations
Table (TABLE) Contains TABLE ROWS (TR) Contains TABLE DATA (TD) Data can contain anything Text Lists Other tables Pictures …
Advertisements

WEB DESIGN TABLES, PAGE LAYOUT AND FORMS. Page Layout Page Layout is an important part of web design Why do you think your page layout is important?
Simple Java Forms Dr. Robert L. Probert S.I.T.E., University of Ottawa.
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
Computing Concepts Advanced HTML: Tables and Forms.
JAVASCRIPT TIPS. REMEMBER JAVASCRIPT IS VERY, VERY CASE SENSITIVE.
Tutorial 5 Working with Web Tables. XP Objectives Explore the structure of a Web table Create headings and cells in a table Create cells that span multiple.
225 City Avenue, Suite 106 Bala Cynwyd, PA , phone , fax presents… HTML Lists, Tables and Forms v2.0.
SERVER web page repository WEB PAGE instructions stores information and instructions BROWSER retrieves web page and follows instructions Server Web Server.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Copyright 2007, Information Builders. Slide 1 Understanding Basic HTML Amanda Regan Technical Director June, 2008.
Tutorial 5 Working with Tables and Columns
Tutorial 5 Working with Tables and Columns
Computer Information Technology – Section 4-12 Some text and examples used with permission from: Note: We not endorsing or promoting.
PHP Form Introduction Getting User Information Text Input.
CIS234A- Lecture 7 Instructor Greg D’Andrea. Tables A table can be displayed on a Web page either in a text or graphical format. A text table: – contains.
Table (TABLE) Contains TABLE ROWS (TR) Contains TABLE DATA (TD) Data can contain anything › Text › Lists › Other tables › Pictures › …
SERVER web page repository WEB PAGE instructions stores information and instructions BROWSER retrieves web page and follows instructions Server Web Server.
CIS234A Lecture 8 Instructor Greg D’Andrea. Review Text Table contains only text, evenly spaced on the Web page in rows and columns uses only standard.
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.
Using Tables for Layout INP150 Session #8. Layout Map out your page Design with paper and pencil Determine number of rows and columns you need Determine.
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.
JavaScript Functions. CSS Inheritance Which formatting applies? x y z input { display: block; } input.pref { background:red; } If you have a selector.
Positioning Objects with CSS and Tables
CSD 340 (Blum)1 Introducing Text Input elements and Ifs.
HTML Elements: Forms and Controls Chapter 9 B. Ramamurthy.
Tutorial 5 Working with Web Tables. New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 2 Objectives Learn and Apply the structure of.
HTML Structure II (Form) WEEK 2.2. Contents Table Form.
HTML Help book. HTML HTML is the programming language used to make web pages for the Internet. HTML stands for Hyper Text Markup Language. HTML is made.
INTRODUCTION ABOUT DIV Most websites have put their content in multiple columns. Multiple columns are created by using or elements. The div element is.
Client-side (JavaScript) Validation. Associating a function with a click event – Part 1 Use the input tag’s onclick attribute to associate a function.
A little more JavaScript??. Building a caculator We'll need to learn about the following: – Form buttons. – Applying CSS rules by attribute values. –
 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.
Tutorial 5 Working with Web Tables
Event-Driven Programming
CNIT 131 HTML5 - Tables.
Organizing Content with Lists and Tables
Applying CSS to Tables Stylish Tables.
Working with Tables: Module A: Table Basics
Loops BIS1523 – Lecture 10.
Positioning Objects with CSS and Tables
Unit M Programming Web Pages with
Tutorial 5 Working with Tables and Columns
Basic XHTML Tables XHTML tables—a frequently used feature that organizes data into rows and columns. Tables are defined with the table element. Table.
Tables, inputs, and variables
JavaScript, continued.
JavaScript Functions.
Tutorial 5 Working with Web Tables
JavaScript Forms Adding User Input.
Console.
Functions BIS1523 – Lecture 17.
Javascript: variables and parameters
Event Driven Programming & User Defined Functions
JavaScript Functions.
Web Design and Development
In Class Programming BIS1523 – Lecture 11.
Reviewing key concepts
Training & Development
Computer communications
JavaScript Basics Topics Review Important Methods Writing Functions
The Internet 10/27/11 XHTML Forms
Introduction to Programming and JavaScript
JavaScript functions.
Lesson 5: HTML Tables.
CS205 Tables & Forms © 2012 D. J. Foreman.
Writing to the Page Formatting Forms
Positioning Objects with CSS and Tables
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 name() { stmt; } Function format function name() { stmt; }

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

JAVASCRIPT FUNCTIONS WITH PARAMETERS Let the function work on any data

Parameters Function

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

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 taller(‘Mutt’,’Jeff’); taller(‘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 document.getElementById(‘here’).inner HTML = function(parm1, parm2); difference = subtract(minuhend,subtrahend); Contrast with alert(string);

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

Statements in a function

Function = recipe Steps = assignments Order matters Perform actions one at a time Gets current values from right side of assignment Manipulates as requested Gives that value to the left side of the statement

Referencing information on an HTML page

3 Parts document = on this page getElementById(‘name’) = identifies the subby hole Which part of the cubby hole? value = the value assigned to an input field src = the file that is to be displayed in an img className = the class used to format the element innerHTML = the HTML within the tag and its end

tables

A Basic Table Table (TABLE) <table> Contains TABLE DATA (TD) <tr> <td> </td> </tr> </table> Table (TABLE) Contains TABLE ROWS (TR) Contains TABLE DATA (TD) Data can contain anything Text Lists Other tables Pictures …

Table borders Every element in the table has a border Adjacent cells can have their own borders (default) or they can share a border (border-collapse)

Formatting elements Often need to repeat format on multiple elements CSS notation th,tr,td { border: none; }

Column Width Tables will adjust columns based on content What if you want them fixed width? Fixed and same td { width: …; } Fixed and different class per td (but have to put it on every td)

Centering Tables They have width. Use margins. Elements in table can have any text align you want Remember display: table;

Merging Cells CAN’T BE DONE IN CSS HTML attributes on td colspan=“n” rowspan=“n”