unit 2 — the exciting world of javascript

26
Unit 2 — The Exciting World of JavaScript Lesson 8 — Using JavaScript with Frames

Upload: jarvis

Post on 05-Jan-2016

30 views

Category:

Documents


1 download

DESCRIPTION

Unit 2 — The Exciting World of JavaScript. Lesson 8 — Using JavaScript with Frames. Objectives. Create a JavaScript function with a parameter list. Create JavaScript-enabled hyperlinks that affect frames. Create JavaScript-enabled buttons that affect frames. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Unit 2 — The Exciting World         of JavaScript

Unit 2 — The Exciting World of JavaScript

Lesson 8 — Using JavaScript with Frames

Page 2: Unit 2 — The Exciting World         of JavaScript

The Exciting World of JavaScript2

Objectives

Create a JavaScript function with a parameter list.

Create JavaScript-enabled hyperlinks that affect frames.

Create JavaScript-enabled buttons that affect frames.

Create top-level JavaScript functions.

Page 3: Unit 2 — The Exciting World         of JavaScript

The Exciting World of JavaScript3

Advanced JavaScript Programming

The parent/child concept: In order for a JavaScript function to access an object in a different file, the two files must be linked. The “parent” frame set can be referenced with a JavaScript object. The frame set file defines “child” frames, and these frames are given names.

To apply this concept, create a JavaScript-ready frame set.

Page 4: Unit 2 — The Exciting World         of JavaScript

The Exciting World of JavaScript4

Advanced JavaScript Programming (cont.)

No. 1: Save HTML code necessary to define the parent/child frame set pages.

<HTML>

<HEAD>

<TITLE>HTML and JavaScript</TITLE>

</HEAD>

<FRAMESET ROWS="140,*">

<FRAME NAME="upperFrame" SRC="upper1.html">

<FRAME NAME="lowerFrame" SRC="lower1.html">

</FRAMESET>

</HTML>

Page 5: Unit 2 — The Exciting World         of JavaScript

The Exciting World of JavaScript5

Advanced JavaScript Programming (cont.)

No. 2: Save the HTML page for the upper frame.

<HTML>

<HEAD>

<TITLE>HTML and JavaScript</TITLE>

</HEAD>

<BODY>

<CENTER>

<IMG NAME="upperImage" SRC="lions.gif">

</CENTER>

</HTML>

Page 6: Unit 2 — The Exciting World         of JavaScript

The Exciting World of JavaScript6

Advanced JavaScript Programming (cont.)

<HTML>

<HEAD>

<TITLE>HTML and

JavaScript</TITLE>

</HEAD>

<BODY>

<CENTER>

<H2>IMAGE LIST</H2>

<TABLE> <TR><TD>1: LIONS.GIF</TD></TR>

<TR><TD>2: TIGERS.GIF</TD></TR>

<TR><TD>3: BEARS.GIF</TD></TR>

<TR><TD>4: OHMY.GIF</TD></TR>

</TABLE>

</CENTER>

</BODY>

</HTML>

No. 3: Save the HTML page for the lower frame.

Page 7: Unit 2 — The Exciting World         of JavaScript

The Exciting World of JavaScript7

Advanced JavaScript Programming (cont.)

Display the js-test16.html file, which displays two files.

upper1.html

lower1.html

Page 8: Unit 2 — The Exciting World         of JavaScript

The Exciting World of JavaScript8

Adding JavaScript Code to Your Frame Set

No. 1: Change the name of the lower frame file.

<HTML>

<HEAD>

<TITLE>HTML and JavaScript</TITLE>

</HEAD>

<FRAMESET ROWS="140,*">

<FRAME NAME="upperFrame" SRC="upper1.html">

<FRAME NAME="lowerFrame" SRC="lower2.html">

</FRAMESET>

</HTML>

Page 9: Unit 2 — The Exciting World         of JavaScript

The Exciting World of JavaScript9

Adding JavaScript Code to Your Frame Set (cont.)

No. 2: Save a new lower2.html file with a JavaScript function using if statements and the parameter (number) to determine which graphic to use in the upper frame.

function setImage(number)

{

if(number == 1)

{

parent.upperFrame.document.upperImage.src="lions.gif";

}

return;

}

Page 10: Unit 2 — The Exciting World         of JavaScript

The Exciting World of JavaScript10

Adding JavaScript Code to Your Frame Set (cont.)

No. 3: The number is taken from this code, which then passes the name of the requested graphic file to the function.

<TR><TD><A HREF="javascript:setImage(1)">1: LIONS.GIF</A></TD></TR>

<TR><TD><A HREF="javascript:setImage(2)">2: TIGERS.GIF</A></TD></TR>

<TR><TD><A HREF="javascript:setImage(3)">3: BEARS.GIF</A></TD></TR>

<TR><TD><A HREF="javascript:setImage(4)">4: OHMY.GIF</A></TD></TR>

Page 11: Unit 2 — The Exciting World         of JavaScript

The Exciting World of JavaScript11

Adding JavaScript Code to Your Frame Set (cont.)

Display the js-test18.html file, which displays two other files.

upper1.html

lower2.html

Page 12: Unit 2 — The Exciting World         of JavaScript

The Exciting World of JavaScript12

Creating a Frame-Based Slide Show

No. 1: Change the name of the lower frame file.<HTML>

<HEAD>

<TITLE>HTML and JavaScript</TITLE>

</HEAD>

<FRAMESET ROWS="140,*">

<FRAME NAME="upperFrame" SRC="upper1.html">

<FRAME NAME="lowerFrame" SRC="lower3.html">

</FRAMESET>

</HTML>

Page 13: Unit 2 — The Exciting World         of JavaScript

The Exciting World of JavaScript13

Creating a Frame-Based Slide Show (cont.)

No. 2: Save a new lower3.html file with new INPUT buttons.

<CENTER>

<H2>WELCOME</H2>

<H3>to my</H3>

<H3>Electronic Slideshow</H3>

<BR>

<INPUT TYPE="BUTTON" VALUE="Prev Image">

<INPUT TYPE="BUTTON" VALUE="Next Image">

</CENTER>

Page 14: Unit 2 — The Exciting World         of JavaScript

The Exciting World of JavaScript14

Creating a Frame-Based Slide Show (cont.)

Display the js-test18.html file, which displays two different files.

upper1.html

lower3.html

Page 15: Unit 2 — The Exciting World         of JavaScript

The Exciting World of JavaScript15

Making Your Slide Show Buttons Functional

No. 1: Change the name of the lower frame file.<HTML>

<HEAD>

<TITLE>HTML and JavaScript</TITLE>

</HEAD>

<FRAMESET ROWS="140,*">

<FRAME NAME="upperFrame" SRC="upper2.html">

<FRAME NAME="lowerFrame" SRC="lower4.html">

</FRAMESET>

</HTML>

Page 16: Unit 2 — The Exciting World         of JavaScript

The Exciting World of JavaScript16

Making Your Slide Show Buttons Functional (cont.)

No. 2: Add the JavaScript image array to the new upper2.html.

var images= new Array(4);

images[0] = new Image;

images[0].src = "lions.gif";

images[1] = new Image;

images[1].src = "tigers.gif";

images[2] = new Image;

images[2].src = "bears.gif";

images[3] = new Image;

images[3].src = "ohmy.gif";

var index=0;

Page 17: Unit 2 — The Exciting World         of JavaScript

The Exciting World of JavaScript17

Making Your Slide Show Buttons Functional (cont.)

No. 3: Add the prevImage() function to upper2.html.

function prevImage(){ if (index > 0) { index--; document.upperImage.src=images[index].src; } return;}

Page 18: Unit 2 — The Exciting World         of JavaScript

The Exciting World of JavaScript18

Making Your Slide Show Buttons Functional (cont.)

No. 4: Add the nextImage() function to upper2.html.

function nextImage(){ if (index <3) { index++; document.upperImage.src=images[index].src; } return;}

Page 19: Unit 2 — The Exciting World         of JavaScript

The Exciting World of JavaScript19

Making Your Slide Show Buttons Functional (cont.)

No. 5: Rewrite lower4.html with these tags.

<INPUT TYPE="BUTTON" VALUE="Prev Image"

onClick="parent.upperFrame.prevImage()">

<INPUT TYPE="BUTTON" VALUE="Next Image"

onClick="parent.upperFrame.nextImage()">

Page 20: Unit 2 — The Exciting World         of JavaScript

The Exciting World of JavaScript20

Making Your Slide Show Buttons Functional (cont.)

Display the js-test19.html file, which displays two changed files.

upper2.html

lower4.html

Page 21: Unit 2 — The Exciting World         of JavaScript

The Exciting World of JavaScript21

Creating a Top-Level JavaScript Function

No. 1: Add code to the parent file.<HTML>

<HEAD>

<TITLE>HTML and JavaScript</TITLE>

<SCRIPT>

function message()

{

alert("We're off to see the Wizard!");

return;

}

</SCRIPT>

Page 22: Unit 2 — The Exciting World         of JavaScript

The Exciting World of JavaScript22

Creating a Top-Level JavaScript Function (cont.)

No. 2: Change the names to the child files.

<FRAMESET ROWS="140,*">

<FRAME NAME="upperFrame"

SRC="upper3.html">

<FRAME NAME="lowerFrame" SRC="lower5.html">

</FRAMESET>

</HTML>

Page 23: Unit 2 — The Exciting World         of JavaScript

The Exciting World of JavaScript23

Creating a Top-Level JavaScript Function (cont.)

No. 3: Convert the image hyperlink in the new upper3html.

<CENTER>

<A HREF="javascript:top.message()"><IMG NAME="upperImage"

SRC="lions.gif"></A>

</CENTER>

Page 24: Unit 2 — The Exciting World         of JavaScript

The Exciting World of JavaScript24

Creating a Top-Level JavaScript Function (cont.)

No. 4: Add the new button to the new lower5.html.

<BR><BR>

<INPUT TYPE="BUTTON" VALUE="Show Message"

onClick="top.message()">

Page 25: Unit 2 — The Exciting World         of JavaScript

The Exciting World of JavaScript25

Creating a Top-Level JavaScript Function (cont.)

Display the js-test20.html file, which displays two altered files.

upper3.html

lower5.html

Page 26: Unit 2 — The Exciting World         of JavaScript

The Exciting World of JavaScript26

Summary

You can create a JavaScript function with a parameter list.

You can create JavaScript-enabled hyperlinks that affect other frames.

You can create JavaScript-enabled buttons that affect other frames.

You can create top-level JavaScript functions.