Docsity

Introduction to Java Script Part IV - Lecture Notes | CGS 3175, Study notes of Computer Science

Partial preview of the text

Download Introduction to Java Script Part IV - Lecture Notes | CGS 3175 and more Study notes Computer Science in PDF only on Docsity!

CGS 3175: Internet Applications (JavaScript – Part 4)

Page 1

© Mark Llewellyn

CGS 3175: Internet Applications

Fall 2007

Introduction To JavaScript – Part 4School of Electrical Engineering and Computer Science

University of Central Florida

Instructor :

Dr. Mark [email protected] 236, 407-823-2790http://www.cs.ucf.edu/courses/cgs3175/fall

CGS 3175: Internet Applications (JavaScript – Part 4)

Page 2

© Mark Llewellyn

26. Modify the example XHTML document onpage 20 so that it uses a function to printTiffany’s name.

Things to Try Yourself

CGS 3175: Internet Applications (JavaScript – Part 4)

Page 4

© Mark Llewellyn

-^ The functions we’ve seen so far have been functions which required noparameters. In other words, we did not need to send any information to thefunction in order for the function to accomplish its task. (Recall that whena function is invoked (called) it simply performs the task it was designed toaccomplish.)•^ In Practice Problem #26 the function printed Tiffany’s name. Suppose thatwe wanted the function to be able to print any visitor’s name.

To

accomplish this, we would need to ask the user to enter their name andthen send their name as a parameter to the function.• Just like the variables we’ve already seen in JavaScript, a parameter is alsoa variable, but this variable belongs only to the function in which it isdefined.• Let’s rewrite Practice Problem #26 using a function with a parameter inwhich we will send the function the visitor’s name.

More On JavaScript Functions

CGS 3175: Internet Applications (JavaScript – Part 4)

Page 5

© Mark Llewellyn

More On JavaScript Functions

^ JavaScript

functions

with^ parameters

function

writeVisitorName(name)

document.write("

"^ +^ name

+^ "^ ");

**} ** Thanks^ for

using^ JavaScript

^

/*^ ^ */

CGS 3175: Internet Applications (JavaScript – Part 4)

Page 7

© Mark Llewellyn

More On JavaScript Functions

CGS 3175: Internet Applications (JavaScript – Part 4)

Page 8

© Mark Llewellyn

•^ To create a slide show on the Web you preloada set of images, which are then played ondemand

as^

the^ visitor

clicks

forward

and

backward buttons.• We’ll add captions to our slides and introducesome

more

JavaScript

features

in

this

Creating A Slide Show Using JavaScriptexample.

CGS 3175: Internet Applications (JavaScript – Part 4)

Page 10

© Mark Llewellyn

Creating A Slide Show Using JavaScript /* The text^ captions

are^ stored

in^ another

array.*/

var^ myCaptions

=^ new^ Array(); myCaptions[1]

=^ "Team^

USA^ -^ Pan^

Am^ Gold^ Medalists.";

myCaptions[2]

=^ "Jennie

Finch^ -^ Team

USA^ pitcher.";

myCaptions[3]

=^ "Jessica

Mendosa^

-^ Team^ USA -^ right^ fielder.";

myCaptions[4]

=^ "Cat^ Osterman

-^ Team^ USA

pitcher.";

myCaptions[5]

=^ "Caitlin

Lowe^ -^ Team

USA^ and^

Arizona^ Wildcat.";

myCaptions[6]

=^ "Taryne

Mowatt^ -^

University

of^ Arizona

Wildcats^

-^ pitcher.";

myCaptions[6]

+=^ "^ ESPN

Female^ Athlete

of^ the^ Year

myCaptions[7]

=^ "Lovie

Jung^ -^ Team

USA^ -^ Sliding

int^ 2nd^

base^ during

2007 World

Cup.";

myCaptions[8]

=^ "Three

great^ pitchers:

Jennie^ Finch,

Alica^ Hollowell,

and^ Cat^ Osterman.";

var^ slidenumber

=^ 1;

var^ totalslides

=^ mySlides.length

-^ 1;

function^

showSlide(direction){if (direction^ ==

"next"){ (slidenumber

==^ totalslides)

?^ slidenumber

=^1 :^ slidenumber++;

}else{(slidenumber

==^ 1)^?^

slidenumber

=^ totalslides

:^ slidenumber--;

} if^ (document.images){document.slideframe.src

=^ mySlides[slidenumber].src; document.slidecontrols.caption.value

=^ myCaptions[slidenumber];

document.slidecontrols.currentslide.value

=^ slidenumber;

} }

CGS 3175: Internet Applications (JavaScript – Part 4)

Page 11

© Mark Llewellyn

Creating A Slide Show Using JavaScript Softball^ Images

2006-2007

^

CGS 3175: Internet Applications (JavaScript – Part 4)

Page 13

© Mark Llewellyn

CGS 3175: Internet Applications (JavaScript – Part 4)

Page 14

© Mark Llewellyn

CGS 3175: Internet Applications (JavaScript – Part 4)

Page 16

© Mark Llewellyn

-^ The

fo the document includes an image called

slideframe

and a from

called^ slidecontrols

.^ Wihtin the form is a textarea called caption set to display 2 rows and 50 characters (columns) of text.

An initial text string informs

the visitor that the slide show begins with a picture of the 2007 Pan Am gold medalteam from the USA.• The form includes two buttons, the first button calls the

showSlide()

function

and sends it the value

previous

.^ The second button calls the

showSlide()

function and sends it the value

next.

-^ The

showSlide()

function in the

takes the value of the parameter it

receives (either

previous

or^ next

) and places it in a local (temporary) variable

named^ direction

. If^ direction

has a value of next, the function looks to see

whether the current slide number (stored in the global variable

slidenumber

) is

the last slide in the show.

If it is,

slidenumber

is reset to 1, Otherwise,

slidenumber

is incremented by 1. If

direction

is^ previous

, the function

checks to see if the current slide number is the first slide in the show.

If it is,

slidenumber

is set to the total number of slides. Otherwise,

slidenumber

is

How The Slide Show Script Worksdecremented by 1. if (direction == "next"){

(slidenumber == totalslides)? slidenumber

= 1 : slidenumber++;

}else{(slidenumber == 1)? slidenumber =

totalslides : slidenumber--;

CGS 3175: Internet Applications (JavaScript – Part 4)

Page 17

© Mark Llewellyn

-^ Finally, the

showSlide()

function checks to make sure that

the browser supports the

images array.

It then uses the

current value of

slidenumber

to determine which element

of the^

mySlides

array and corresponding element of the myCaptions

array to display.

The^ slidenumber

is also

displayed in the text field called

currentslide

so that the

visitor will see the number of the current slide.• This slide show technique is useful for building pages whereyou want the visitor to proceed through a series of images byclicking

buttons.

Corporate

presentations,

travel

logs,

How The Slide Show Script Worksinstructional demonstrations are just some of the possibleapplications.

CGS 3175: Internet Applications (JavaScript – Part 4)

Page 19

© Mark Llewellyn

-^ The table heading element

^ is used to label the rows and columns of

a table.^

This element is an optional element for tables, as some tables do not require headings.

In most browsers the

^ element automatically

centers the content of the element within the table’s cell and makes the textbold.• The main body of a table is made up of rows and columns, like a matrix.In XHTML, you construct your tables one row at a time. Each row beginswith an opening table row

^

tag.^ Each column within that row

contains the open and end tags for the table data

^ element. After the

last^

element is closed for the last column, the row ends with the ending^

tag.^ Shown below is the definition for a row that contains three columns.

XHTML Tables

column

1 ^ column

2 ^ column

3

CGS 3175: Internet Applications (JavaScript – Part 4)

Page 20

XHTML Table –Example 1^ © Mark Llewellyn

Table^ Example

in^ XHTML Our

First^ Table

A

Simple^ Table

of^ Columns

and^ Rows

Column

1 Column

2 Column

3

Column

1 ^

Row^ 2 Column

2 ^

Row^ 2 Column

3 ^

Row^ 2

Column

1 ^

Row^ 3 Column

2 ^

Row^ 3 Column

3 ^

Row^ 3